16ms Java Solution


  • 0
    J

    '''
    public class Solution {

    public int findKthLargest(int[] nums, int k)  {
        
       PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
       
       for(int n : nums)
       {
          pq.offer(n);
          
          //Queue will always hold a maximum of K values
          if(pq.size()>k)
            pq.poll();
       }
       
       int res=0;
       
       //Remove pq.size()-k elements.
       while(pq.size()!=k)
            pq.poll();
        
        return pq.poll();
    }   
    

    }
    '''


Log in to reply
 

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