Easy to understand Java Solution


  • 0
    N
    public class Solution {
    public int findKthLargest(int[] nums, int k) {
        Queue<Integer> priorityQueue = new PriorityQueue<>(k);
        for(int i:nums){
        	if(priorityQueue.size()==k){
        		if(priorityQueue.peek()<i){
        			priorityQueue.poll();
        			priorityQueue.add(i);
        		}
        	}else{
        		priorityQueue.add(i);
        	}
        	
        }
        return priorityQueue.peek();
    }
    

    }


Log in to reply
 

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