Simple Java Solution using Hash and sorting


  • 0
    A
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
            //Storing the scores and their initial occurrence in a hash map.
            for(int i=0;i<nums.length;i++)
                map.put(nums[i],i);
    
            Arrays.sort(nums);
            String[] res = new String[nums.length];
            for(int i=0;i<nums.length-3;i++){
                int initial = map.get(nums[i]);
                res[initial] = Integer.toString(nums.length - i); 
            }
            if(nums.length>2){
                int b = map.get(nums[nums.length-3]);
                res[b] = "Bronze Medal";
            }
            if(nums.length>1){
                int s = map.get(nums[nums.length-2]);
                res[s] = "Silver Medal";
            }
            int g = map.get(nums[nums.length-1]);
            res[g] = "Gold Medal";
            return res;
        }
    }
    

Log in to reply
 

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