Java Solution using Map and Sort


  • 1
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            
            if(nums == null || nums.length == 0)
                return new String[0];
                
            Map<Integer,Integer> data = new HashMap<Integer,Integer>();
            String[] relative_ranks = new String[nums.length];
         
         
            for(int i=0;i<nums.length;++i)
            {
                data.put(nums[i],i);
            }
            
            Arrays.sort(nums);
         
            String[] medals = new String[]{"Gold Medal","Silver Medal","Bronze Medal"};
         
            for(int i=nums.length-1;i>-1;--i)
            {
                relative_ranks[data.get(nums[i])] = nums.length-i <= 3 ? medals[nums.length-i-1] : String.valueOf(nums.length - i);
            }
            
            
            return relative_ranks;
        }
    }
    

Log in to reply
 

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