|
5 | 5 | import java.util.Map;
|
6 | 6 |
|
7 | 7 | /**
|
8 |
| - * Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal". |
| 8 | + * 506. Relative Ranks |
| 9 | + * |
| 10 | + * Given scores of N athletes, find their relative ranks and the people with the top three highest scores, |
| 11 | + * who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal". |
9 | 12 |
|
10 | 13 | Example 1:
|
11 | 14 | Input: [5, 4, 3, 2, 1]
|
|
19 | 22 | */
|
20 | 23 | public class _506 {
|
21 | 24 |
|
22 |
| - public String[] findRelativeRanks(int[] nums) { |
23 |
| - int[] tmp = new int[nums.length]; |
24 |
| - for (int i = 0; i < nums.length; i++) { |
25 |
| - tmp[i] = nums[i]; |
26 |
| - } |
27 |
| - Arrays.sort(tmp); |
28 |
| - Map<Integer, String> rankMap = new HashMap(); |
29 |
| - int len = nums.length; |
30 |
| - for (int i = len - 1; i >= 0; i--) { |
31 |
| - if (i == len - 1) { |
32 |
| - rankMap.put(tmp[i], "Gold Medal"); |
33 |
| - } else if (i == len - 2) { |
34 |
| - rankMap.put(tmp[i], "Silver Medal"); |
35 |
| - } else if (i == len - 3) { |
36 |
| - rankMap.put(tmp[i], "Bronze Medal"); |
37 |
| - } else { |
38 |
| - rankMap.put(tmp[i], String.valueOf(len - i)); |
| 25 | + public static class Solution1 { |
| 26 | + public String[] findRelativeRanks(int[] nums) { |
| 27 | + int[] tmp = new int[nums.length]; |
| 28 | + for (int i = 0; i < nums.length; i++) { |
| 29 | + tmp[i] = nums[i]; |
39 | 30 | }
|
| 31 | + Arrays.sort(tmp); |
| 32 | + Map<Integer, String> rankMap = new HashMap(); |
| 33 | + int len = nums.length; |
| 34 | + for (int i = len - 1; i >= 0; i--) { |
| 35 | + if (i == len - 1) { |
| 36 | + rankMap.put(tmp[i], "Gold Medal"); |
| 37 | + } else if (i == len - 2) { |
| 38 | + rankMap.put(tmp[i], "Silver Medal"); |
| 39 | + } else if (i == len - 3) { |
| 40 | + rankMap.put(tmp[i], "Bronze Medal"); |
| 41 | + } else { |
| 42 | + rankMap.put(tmp[i], String.valueOf(len - i)); |
| 43 | + } |
| 44 | + } |
| 45 | + String[] result = new String[len]; |
| 46 | + for (int i = 0; i < len; i++) { |
| 47 | + result[i] = rankMap.get(nums[i]); |
| 48 | + } |
| 49 | + return result; |
40 | 50 | }
|
41 |
| - String[] result = new String[len]; |
42 |
| - for (int i = 0; i < len; i++) { |
43 |
| - result[i] = rankMap.get(nums[i]); |
44 |
| - } |
45 |
| - return result; |
46 | 51 | }
|
47 | 52 |
|
48 | 53 | }
|
0 commit comments