Data Structures19 sections · 729 units
Open in Course

Lessons from Kth Largest

Heap size matters

What did you learn?

1.1. For k-th largest, use a min-heap of size kk. For k-th smallest, use a max-heap of size kk.

2.2. The heap root is the "boundary" element. Anything smaller (for k-th largest) gets filtered out.

3.3. Bounded heaps reduce time complexity from O(nlogn)O(n \log n) to O(nlogk)O(n \log k). This pattern extends to "top k elements," "k closest points," and similar problems.