algorythms
Heap / Priority Queue
LC #215Medium

Kth Largest Element in an Array

Heap / Priority Queue
MetaAmazonGoogleBloombergMicrosoft

Problem

Find the kth largest element in an unsorted array (not kth distinct).

arrayheapquickselectsorting

Constraints

  • 1 ≤ k ≤ nums.length ≤ 10⁵
  • -10⁴ ≤ nums[i] ≤ 10⁴

Example

Inputnums = [3, 2, 1, 5, 6, 4], k = 2
Output5
Why

Sorted descending: [6,5,4,3,2,1]. 2nd largest is 5.

Hints — reveal one at a time