Heap / Priority Queue
LC #215Medium
Kth Largest Element in an Array
Heap / Priority Queue
MetaAmazonGoogleBloombergMicrosoftProblem
Find the kth largest element in an unsorted array (not kth distinct).
arrayheapquickselectsorting
Constraints
- ›1 ≤ k ≤ nums.length ≤ 10⁵
- ›-10⁴ ≤ nums[i] ≤ 10⁴
Example
Input
nums = [3, 2, 1, 5, 6, 4], k = 2Output
5Why
Sorted descending: [6,5,4,3,2,1]. 2nd largest is 5.