Easy Java Solution, Using HashMap.


  • 1

    Using Map to mark score position.

    public class Solution {
    
        public String[] findRelativeRanks(int[] nums) {
            HashMap<Integer, Integer> posMap = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                posMap.put(nums[i], i);
            }
    
            Arrays.sort(nums);
    
            String[] ans = new String[nums.length];
            int last = nums.length-1;
            
            ans[posMap.get(nums[last--])] = "Gold Medal";
            if (last < 0) return ans;
            ans[posMap.get(nums[last--])] = "Silver Medal";
            if (last < 0) return ans;
            ans[posMap.get(nums[last--])] = "Bronze Medal";
            if (last < 0) return ans;
            
            int n = 4;
            for (int i = last; i >= 0; i--) {
                ans[posMap.get(nums[i])] = String.valueOf(n++);
            }
            return ans;
        }
    }
    

  • 0

    @Bronya It’s easy!


Log in to reply
 

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