Relative Ranks

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".

Example 1:

Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal". 
For the left two athletes, you just need to output their relative ranks according to their scores.

Note:

  1. N is a positive integer and won't exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.
 1 public class Solution {
 2     public String[] findRelativeRanks(int[] nums) {
 3         int n = nums.length;
 4         
 5         HashMap map = new HashMap();
 6         for (int i = 0; i < n; i++) {
 7             map.put(nums[i], i);
 8         }
 9         
10         Arrays.sort(nums);
11         String[] result = new String[n];
12         
13         for (int i = 0; i < n; i++) {
14             int index = (int) map.get(nums[i]);
15             
16             if (i == n - 1) result[index] = "Gold Medal";
17             else if (i == n - 2) result[index] = "Silver Medal";
18             else if (i == n - 3) result[index] = "Bronze Medal";
19             else result[index] = Integer.toString(n - i);
20         }
21         return result;
22     }
23 }
原文地址:https://www.cnblogs.com/amazingzoe/p/6391364.html