Java O(nlogn) time using sort and HashMap


  • 0
    T
    public String[] findRelativeRanks(int[] nums) {
    		String[] ret = new String[nums.length];
    		HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
    		for (int i = 0; i < nums.length; i++)
    			hm.put(nums[i], i);
    
    		Arrays.sort(nums);
    		
    		int i = 0;
    		for (; i < nums.length - 3; i++)
    			ret[hm.get(nums[i])] = nums.length - i + "";
    		ret[hm.get(nums[i])] = "Bronze Medal";
    		i++;
    		ret[hm.get(nums[i])] = "Silver Medal";
    		i++;
    		ret[hm.get(nums[i])] = "Gold Medal";
    
    		return ret;
    	}

Log in to reply
 

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