Monotonic stack
Also called: Monotonic queue, Monotonic deque
A stack kept in sorted order of value, used to find the next or previous greater or smaller element for every position in one pass.
Definition
Each new element pops every waiting element it beats, which gives those elements their answer, then waits itself. Every element is pushed and popped at most once, so the whole pass is O(n). A monotonic deque applies the same idea to sliding windows, keeping the window's maximum or minimum at the front.
In practice
Daily Temperatures: for each day, the number of days until a warmer one, computed in a single left-to-right scan.
Learn it in context
Lessons that use this term.