[JAVA] HashMap with previous index / T : O(NLogN), S : O(N)


  • 0
    J
    class Solution {
        public String[] findRelativeRanks(int[] nums) {
            String[] result = new String[nums.length];
            Integer[] vals = new Integer[nums.length];
            HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
            for(int i = 0; i < nums.length; i++){
                map.put(nums[i], i);
                vals[i] = nums[i];
            }
            
            Arrays.sort(vals, Collections.reverseOrder());
            
            result[map.get(vals[0])] = "Gold Medal";
            if(result.length > 1)
                result[map.get(vals[1])] = "Silver Medal";
            if(result.length > 2)
                result[map.get(vals[2])] = "Bronze Medal";
            
            for(int i = 3; i < vals.length; i++){
                result[map.get(vals[i])] = String.valueOf(i+1);
            }
            return result;
        }
    }
    

Log in to reply
 

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