Stage 1 · Code
Heaps & Priority Queues
Max Heap
Max heap implementation and applications.
Max Heap
Max heap: parent ≥ children. Root is maximum. Implementation identical to min heap with reversed comparisons. Can wrap min heap by negating values.
Go's container/heap
Go's container/heap provides heap operations for any type implementing heap.Interface (Len, Less, Swap, Push, Pop). It's a min heap by default; reverse Less for max heap.
Applications
- Priority queue: Max heap for highest priority first.
- Kth largest: Min heap of size k (keep k largest).
- Top K frequent: Count frequencies, heap of size k.
- Median of stream: Two heaps (max for lower half, min for upper half).
- Task scheduling: Highest priority task first.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.