Kth Largest Element in an Array 2021-06-16 19:55

Problem Description

public int findKthLargest(int[] nums, int k) {
    PriorityQueue<Integer> minHeap = new PriorityQueue<>((a, b) -> a.compareTo(b));
    for (int i = 0; i < nums.length; i++) {
        minHeap.add(nums[i]);
        if (minHeap.size() > k) {
            minHeap.poll();
        }
    }
    return minHeap.poll();
}
Runtime Memory
4 ms 39 MB

henryxi leetcode list

EOF