|
6 | 6 | public class RelativeRanks {
|
7 | 7 |
|
8 | 8 | public String[] findRelativeRanks(int[] nums) {
|
9 | | -Map<Integer,String> map = new HashMap<>(); |
10 | | -String[] str = new String[nums.length]; |
11 | | -int n = nums.length-1; |
12 | | -int[] copy = new int[n+1]; |
13 | | -for(int i=0; i<= n; i++){ |
14 | | - copy[i]= nums[i]; |
15 | | -} |
16 | | -mergeSort(nums, 0, nums.length-1); |
17 | | -for(int i=0; i< nums.length; i++){ |
18 | | - if(i==0){ |
19 | | - map.put(nums[n-i], "Gold Medal"); |
20 | | - } |
21 | | - else if(i==1){ |
22 | | - map.put(nums[n-i], "Silver Medal"); |
23 | | - } |
24 | | - else if(i==2){ |
25 | | - map.put(nums[n-i], "Bronze Medal"); |
26 | | - } |
27 | | - else{ |
28 | | - map.put(nums[n-i], (i+1)+""); |
29 | | - } |
30 | | -} |
31 | | -for(int i=0; i< copy.length; i++){ |
32 | | - str[i] = map.get(copy[i]); |
33 | | -} |
34 | | -return str; |
| 9 | +Map<Integer,String> map = new HashMap<>(); |
| 10 | +String[] str = new String[nums.length]; |
| 11 | +int n = nums.length-1; |
| 12 | +int[] copy = new int[n+1]; |
| 13 | +for(int i=0; i<= n; i++){ |
| 14 | + copy[i]= nums[i]; |
| 15 | +} |
| 16 | +mergeSort(nums, 0, nums.length-1); |
| 17 | +for(int i=0; i< nums.length; i++){ |
| 18 | + if(i==0){ |
| 19 | + map.put(nums[n-i], "Gold Medal"); |
| 20 | + } |
| 21 | + else if(i==1){ |
| 22 | + map.put(nums[n-i], "Silver Medal"); |
| 23 | + } |
| 24 | + else if(i==2){ |
| 25 | + map.put(nums[n-i], "Bronze Medal"); |
| 26 | + } |
| 27 | + else{ |
| 28 | + map.put(nums[n-i], (i+1)+""); |
| 29 | + } |
| 30 | +} |
| 31 | +for(int i=0; i< copy.length; i++){ |
| 32 | + str[i] = map.get(copy[i]); |
| 33 | +} |
| 34 | +return str; |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | public void mergeSort(int[] nums, int l, int p){
|
|
0 commit comments