Java O(NLogN) solution using LinkedHashMap()


  • 0
    S
     public String[] findRelativeRanks(int[] nums) {
            Map<Integer, String> map = new LinkedHashMap<>();
            String[] rank = new String[nums.length];
            int index = 4;
            for(int i: nums) map.put(i, "");
            Arrays.sort(nums);
            for(int i = nums.length - 1; i >= 0; i--){
                if(i == nums.length - 1) map.put(nums[i], "Gold Medal");
                if(i == nums.length - 2) map.put(nums[i], "Silver Medal");
                if(i == nums.length - 3) map.put(nums[i], "Bronze Medal");
                if(i < nums.length - 3) map.put(nums[i], String.valueOf(index++));
            }
            int indexOfRank = 0;
            for(String j: map.values()){
                rank[indexOfRank++] = j;
            }
            return rank;
        }
    

  • 0
    S

    @shakexin The linkedHashMap will keep the inserting order of keys.


Log in to reply
 

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