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".
class Solution {
public String[] findRelativeRanks(int[] nums) {
if(nums == null || nums.length == 0)
return new String[0];
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(new Comparator<Integer>(){
public int compare(Integer i1, Integer i2) {
return i2 - i1;
}
});
for(int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
String[] res = new String[nums.length];
int i = 0;
for(Map.Entry<Integer, Integer> entry: map.entrySet()) {
if(i == 0) {
res[entry.getValue()] = "Gold Medal";
} else if(i == 1) {
res[entry.getValue()] = "Silver Medal";
} else if(i == 2) {
res[entry.getValue()] = "Bronze Medal";
} else {
res[entry.getValue()] = String.valueOf(i + 1);
}
i++;
}
return res;
}
}