Java Solution Using Priority Queue


  • 0
    J
    import java.util.Comparator;
    import java.util.PriorityQueue;
    
    public class Solution {
    	public String[] findRelativeRanks(int[] nums) {
    		if (nums == null || nums.length == 0) {
    			return new String[0];
    		}
    		String[] result = new String[nums.length];
    		PriorityQueue<Pairs> queue = new PriorityQueue<Pairs>(new Comparator<Pairs>() {
    
    			@Override
    			public int compare(Pairs o1, Pairs o2) {
    				return o2.score - o1.score;
    			}
    		});
    		for (int i =0; i < nums.length; i++) {
    			queue.add(new Pairs(i, nums[i]));
    		}
    		int currRank = 1;
    		while (!queue.isEmpty()) {
    			Pairs pair = queue.poll();
    			result[pair.index] = "" + currRank;
    			if (currRank == 1) {
    				result[pair.index] = "Gold Medal";
    			} else if (currRank == 2) {
    				result[pair.index] = "Silver Medal";
    			} else if (currRank == 3) {
    				result[pair.index] = "Bronze Medal";
    			}
    			currRank++;
    		}
    		return result;
    	}
    
    	class Pairs {
    		int index;
    		int score;
    
    		public Pairs(int index, int score) {
    			this.index = index;
    			this.score = score;
    		}
    	}
    }
    

Log in to reply
 

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