Simple Java Solution using TreeMap


  • 0
    J
    public String[] findRelativeRanks(int[] nums) {
            TreeMap<Integer, Integer> map = new TreeMap<>();
            for (int i = 0; i < nums.length; i++) {
                map.put(nums[i], i);
            }
            String[] res = new String[nums.length];
            String[] medals = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal"};
            for (int i = 0; i < nums.length; i++) {
                Map.Entry<Integer, Integer> entry = map.pollLastEntry();
                if (i < 3)
                    res[entry.getValue()] = medals[i];
                else
                    res[entry.getValue()] = String.valueOf(i + 1);
            }
            return res;
        }
    

    Time Complexity: O(nlogn).
    Space Complexity: O(n).


Log in to reply
 

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