AlgoScope

Monotonic Queue

Data structureintermediateLinear Structures

A deque kept sorted so the window maximum is always at the front.

Decision · step 3 of 23Sliding Window Maximum: Maximum of every 3
active 0-11031-12-3354356677i1 < 3

3 enters. The back of the deque is 1, which is not larger than 3 and will leave the window sooner. It can never be the maximum again, so pop it.

Open in the player →or start at step 3

What you will see

Values that can never be a maximum are dropped from the back as the window slides.

How sliding window maximum works →

Cost

push with popsO(1) amortized
pop frontO(1)
SpaceO(k)

How you work with it here

play it through, step one change at a time, scrub to any step, run it on your own input, try operations in any order.

Screen readers: Each element is an accessibility element with position, value and state; structural changes are announced per step.

Reduced motion: Elements appear at their destination with a crossfade; no travel longer than the element's own size.

Before this

Leads to

Topics that need this one first.

Taught by the same lesson

Sliding Window Maximum covers these too, in the same run.