Heaps mind map
Priority queues, heap operations, and top-K.
Core idea: Use a heap when you need the best next item repeatedly, not a fully sorted collection.
Mnemonic: Push frontier → pop best → repair · Cost: O(log k) update · O(1) best
Limit: Top K
Signal: Only the k strongest items matter out of a much larger stream.
Move: Keep a size-k heap and eject the least useful item whenever the limit is exceeded.
Examples: Kth Largest, Top K Frequent, K Closest Points
Merge: K sorted frontiers
Signal: Several ordered sources expose one next candidate each.
Move: Heap one head per source; pop globally smallest and replace it from the same source.
Examples: Merge K Lists, Smallest Range, K-Way Merge
Balance: Two heaps
Signal: An online stream needs its middle or rank boundary after every update.
Move: Max-heap the lower half, min-heap the upper half, and rebalance sizes.
Examples: Median From Stream, Sliding Median, Rank Tracker
Frontier: Scheduling & cheapest path
Signal: The next resource, event, or route is whichever has the smallest key.
Move: Pop the earliest or cheapest live state, update it, then reinsert if it remains active.
Examples: Meeting Rooms III, Task Scheduler, Dijkstra