Easy Priority Queue Jav Solution


  • 0
    V
    public class Solution {
        public String[] findRelativeRanks(int[] nums) {
        //Define Priority Queue in Reverse order  
       PriorityQueue<Integer> pq = new PriorityQueue<Integer>(nums.length,Collections.reverseOrder());
    	//Map to store the index ordering
                Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    	String[] result = new String[nums.length];
    	//Add elements to the queue
    	for(int i=0;i<nums.length;i++){
    		pq.add(nums[i]);
    	}
    	//populate the map with the index
    	for(int i=0;i<nums.length;i++){
    		map.put(pq.poll(),i+1);
    	}
                //extract max and find the index in the map
    	for(int i=0;i<nums.length;i++){
    		int val = map.get(nums[i]);
    		if(val == 1)
    			result[i] = "Gold Medal";
    		else if(val == 2)
    			result[i] = "Silver Medal";
    		else if(val == 3)
    			result[i] = "Bronze Medal";
    		else
    			result[i]=""+val;
    				
    	}
    	return result;
    }
    }

Log in to reply
 

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