Java solution beats 80%


  • 0
    L

    Time complexity will be O(n)+O(nlogn)== O(nlogn) and the space complexity is O(n).

    public String[] findRelativeRanks(int[] nums) {
            Map<Integer,Integer> map = new HashMap<>();
            for(int i=0;i<nums.length;i++) {
                map.put(nums[i],i);
            }
            Arrays.sort(nums);
            String[] arr = new String[nums.length];
            if(nums.length-1>=0)
                arr[map.get(nums[nums.length-1])]="Gold Medal";
            if(nums.length-1>=1)
                arr[map.get(nums[nums.length-2])]="Silver Medal";
            if(nums.length-1>=2)
                arr[map.get(nums[nums.length-3])]= "Bronze Medal";
            for(int i=nums.length-4;i>=0;i--) {
               arr[map.get(nums[i])]=""+(nums.length-i);
            }
            
            return arr;
        }
    

Log in to reply
 

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