Min-heap of size k
Time O(n log k) Space O(k)Keep the k largest seen so far in a min-heap; its top is the answer.
import java.util.PriorityQueue;
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int x : nums) {
heap.offer(x);
if (heap.size() > k) heap.poll();
}
return heap.peek();
}
}Verdict: Simple and good for streams.