Java Solution using Priority Queue


  • 0
    I
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
            String[] res = new String[nums.length];
            
            PriorityQueue<Integer> maxRank = new PriorityQueue(nums.length, Collections.reverseOrder());
            for (int i = 0; i < nums.length; i++) {
                maxRank.add(nums[i]);
            }
            Map<Integer, Integer> a = new HashMap<>();
            int z = 1;
            while (!maxRank.isEmpty()) {
                int i = maxRank.poll();
                a.put(i, z++);
            }
            for (int i = 0; i < nums.length; i++) {
                int rank = a.get(nums[i]);
                if (rank == 1) {
                    res[i] = "Gold Medal";
                } else if (rank == 2) {
                    res[i] = "Silver Medal";
                } else if (rank == 3) {
                    res[i] = "Bronze Medal";
                } else {
                    res[i] = String.valueOf(rank);
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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