42. Kth Largest Element in an Array

Return the k-th largest element in nums (in sorted order, not the k-th distinct value). Can you avoid sorting the whole array?

Example 1

Input: nums = [3,2,1,5,6,4], k = 2
Output: 5

Example 2

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4
Hint 1

Keep only the k largest values seen so far in a min-heap.

Hint 2

The heap’s top is then the k-th largest.

HeapQuickselect