Kth largest element


  • 0
    T

    public int findKthLargest(int[] nums, int k) {
    PriorityQueue<Integer> maxHeap=new PriorityQueue<>((a,b)->b-a);
    for(int x:nums)
    maxHeap.add(x);
    int ans=Integer.MIN_VALUE;
    while(k-->0)
    {
    ans=maxHeap.poll();
    }
    return ans;
    }


Log in to reply
 

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