Java solution using TreeMap


  • 0
    T
    public String[] findRelativeRanks(int[] nums) {
        String[] res = new String[nums.length];
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>(Collections.reverseOrder());
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        
        int idx = 1;
        for (Integer n : map.values()) {
            if (idx == 1) {
                res[n] = "Gold Medal";
            } else if (idx == 2) {
                res[n] = "Silver Medal";
            } else if (idx == 3) {
                res[n] = "Bronze Medal";
            } else {
                res[n] = String.valueOf(idx);
            }
            idx++;
        }
        return res;
    }

Log in to reply
 

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