LeetCode ยท Heap
Heap / Priority Queue Problem Set
Min-heaps, max-heaps, two-heap patterns, and heap-powered graph algorithms. When you need "the K-th largest" or "the best available" โ reach for a heap.
Concept page: Heap / Priority Queue
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | design | LC 703 ยท Kth Largest Element in a Stream | Min-heap of size k. Peek = kth largest. O(log k) per add. |
| Easy | simulation | LC 1046 ยท Last Stone Weight | Max-heap, smash two largest repeatedly. O(n log n). |
| Medium | top-k | LC 215 ยท Kth Largest Element in an Array | Min-heap of size k, or quickselect. O(n) average. |
| Medium | top-k | LC 347 ยท Top K Frequent Elements | Frequency map + min-heap of size k, or bucket sort. O(n). |
| Medium | top-k | LC 973 ยท K Closest Points to Origin | Max-heap of size k by distance. O(n log k). |
| Medium | design | LC 355 ยท Design Twitter | Merge k sorted feeds with max-heap. O(k log k) per getNewsFeed. |
| Medium | greedy+heap | LC 621 ยท Task Scheduler | Max-heap of frequencies + cooldown queue. O(n). |
| Medium | graph+heap | LC 743 ยท Network Delay Time | Dijkstra with a min-heap of (distance, node). Pop nearest first, skip stale entries. |
| Hard | merge | LC 23 ยท Merge k Sorted Lists | Min-heap of k list heads. O(n log k). |
| Hard | two-heap | LC 295 ยท Find Median from Data Stream | Max-heap (lower half) + min-heap (upper half). O(log n) per add, O(1) median. |