← Visualizers
Sliding Window — Monotonic Deque LeetCode 239

Sliding Window Maximum / visualized

The max of every length-k window, in O(n). A monotonic deque holds candidate indices with values in decreasing order — so the front is always the current window's max. Each element is pushed and popped at most once, which is what beats the naïve O(n·k).

Execution

idle
← front (max)back →
stores indices; values shown for clarity · always decreasing front → back
Press Run to begin.
0 / 0
Speed

Java · running line

in window in deque front = max evicted / popped
O(n) time · O(k) space  ·  each index enters the deque once and leaves once — amortized O(1) per element despite the inner while loop.