Module 22 Β· Data Structures & Problem-Solving β Lesson 6 of 7 Β· ~11 min
Heaps & Priority Queues
Always grab the best one, cheaply
A priority queue answers one question fast: what's the smallest (or largest) item right now? You keep pushing items in any order, and each pop hands you the current min (or max).
The structure underneath is a heap β a tree kept partly ordered so the min (or max) is always at the root. Push and pop are O(log n); peeking at the min is O(1).
Real uses: dispatch the nearest available walker first; run the highest-priority job before the rest.