Practice Discrete Math

Algorithms / Monotonic Queue Sliding Window

Least You Need to Know: Monotonic Queues and Sliding-Window Extremes

A monotonic deque keeps the current window's best candidates in order, so the front always holds the maximum or minimum. It is the standard pattern for sliding-window extrema because it supports both expiration from the left and dominance cleanup from the right.

The least you need to know

Key notation

window [L, R] current contiguous range under consideration
deque front current max or min for the window
dominated can never win while a better later candidate remains valid

Tiny worked example

  • For sliding-window maximum, maintain deque values in decreasing order.
  • Before appending a new index, pop smaller values from the back because the new value dominates them.
  • When the left boundary moves, pop the front if that index expired.
  • Then the front is the current window maximum.

Common mistakes

How to recognize this kind of problem

Start practice