Order-focused structures: LIFO, FIFO, and priority
These are order-focused data structures used to control how elements are added and removed:
Time/Space (typical implementations):
Use a:
k
largest/smallest, merge k sorted lists, Dijkstra, A*.1. Monotonic Stack/Queue Maintain increasing/decreasing order to get next greater/smaller in O(n): daily temperatures, largest rectangle in histogram.
2. Sliding Window with Deque Track candidates for max/min in window in O(n) total: sliding window maximum.
3. Heaps for Selection/Aggregation
Top-K
: maintain a size-k
heap.Kth
smallest/largest: heap or quickselect.4. Graphs and Scheduling
5. Emulating Recursion Use an explicit stack to avoid recursion depth limits.
Stack-based:
Queue/Deque-based:
Heap-based:
k
heap for O(n log k).k
or two-heaps)