Java TreeMap Sorted Solution


  • 0
    Y
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            String[] ranks = new String[nums.length];
            Map<Integer, Integer> indexMap = new TreeMap<>(Collections.reverseOrder());
            for (int i = 0; i < nums.length; i++)
                indexMap.put(nums[i], i);
            
            int i = 1;
            for (Map.Entry<Integer, Integer> entry : indexMap.entrySet()) {
                if (i == 1) {
                    ranks[entry.getValue()] = "Gold Medal";
                } else if (i == 2) {
                    ranks[entry.getValue()] = "Silver Medal";
                } else if (i == 3) {
                    ranks[entry.getValue()] = "Bronze Medal";
                } else {
                    ranks[entry.getValue()] = Integer.toString(i);
                }
                i++;
            }
            return ranks;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.