Java code using TreeMap


  • 0
    R
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            String[] result = new String[nums.length];
            TreeMap<Integer,Integer> tm = new TreeMap<Integer,Integer>();
            for(int i=0;i<nums.length;i++) {
                tm.put(nums[i],i);
            }
            for(int i=0;i<nums.length;i++) {
                int cur = tm.lastKey();
                int index = tm.get(cur);
                nums[index] = i+1;
                if (nums[index]>3) result[index] = Integer.toString(nums[index]);
                else if (nums[index]==1) result[index] = "Gold Medal";
                else if (nums[index]==2) result[index] = "Silver Medal";
                else if (nums[index]==3) result[index] = "Bronze Medal";
                tm.remove(cur);
            }
            
            return result;
        }
    }
    

Log in to reply
 

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