To find the maximum in every sliding window, the naive way rescans each window: O(nk), re-reading values again and again. The monotonic queue refuses to re-read. It keeps a double-ended queue of candidates in decreasing order; when a new value arrives it evicts from the back every element it dominates (they can never again be a maximum — the newcomer is bigger and younger), then it appends. The front is always the window's maximum. The eviction is the trick: each element is pushed once and popped once, so the whole sweep is O(n), with a deque bounded by the window. Dominated candidates are thrown away and never revisited — that discarding is the mechanism.
A stream and a window of 3. The demo maintains the monotonic deque and reports each window's maximum, showing the evictions: live demo
“Window maxima cost O(nk).” — the monotonic deque makes it O(n): each value is admitted once and evicted once, and a dominated candidate is discarded forever the moment a bigger, younger one appears. cited
A queue that only ever holds those still in the running; the rest are cast out and never seen again. Linear work from ruthless forgetting. monotonic deque
On the canonical compiler, the maximum of the last window of [1,3,−1,−3,5,3] is 5 — the dominated candidates having been evicted: