The only difference between min and max heaps is which element is at the root. Choosing the right one depends on what you need quick access to.| Property | Min Heap | Max Heap |
|---|
| Root element | Smallest | Largest |
| Parent vs children | Parent ≤ children | Parent ≥ children |
| Pop returns | Minimum element | Maximum element |
- K Largest Elements: Maintain heap of size K, smallest of the K largest is at root
- Dijkstra's Algorithm: Always process the closest unvisited node
- Merge K Sorted Lists: Get the smallest element among all list heads
- Task Scheduling: Process earliest deadline first
- K Smallest Elements: Maintain heap of size K, largest of the K smallest is at root
- Priority Queue: Process highest priority first
- Maximum in Sliding Window: Track the maximum in current window
A common pattern: to find K largest elements, use a min-heap of size K. This seems counterintuitive but makes sense:Some problems use both heaps together, like finding the running median:- Need the smallest? → Min heap
- Need the largest? → Max heap
- Need K largest? → Min heap of size K
- Need K smallest? → Max heap of size K
- Need median? → Both heaps