JAVA Easy Solution using Map


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

Log in to reply
 

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