Heap / Priority Queue · Medium

Kth Largest Element in an Array

O(N log K) · O(K)

Step 1: Setup & Initialization - Kth Largest Element

Maintain a Min-Heap of size K while iterating array.

Array Elements & Pointers
10
[0]
20
[1]
30
[2]
40
[3]
50
[4]
Live Variables & Invariants
status:Initialized
pattern:Kth Largest Element
Step 1 / 3
33%
Solution Code
1
function findKthLargest(nums: number[], k: number): number {
2
  nums.sort((a, b) => b - a);
3
  return nums[k - 1];
4
}
LeetCode IDE Console

Test Cases

3 cases from Blind 75 & NeetCode 150

Ask for a hint whenever you get stuck.