teacher
Sign in

Stacks, Queues and Monotonic Structures

Use a stack for matching and "nearest greater" questions and a deque for sliding-window maximum, each in a single linear pass.

40 minCF 1000–16005 interactive11 problems

The problem

Three questions, one family:

  1. Is a string of brackets like ([]{}) correctly nested?
  2. For each element, where is the next element to its right that is larger?
  3. For every window of k consecutive elements, what is the maximum?

Each asks about the most recent unresolved item, or the best item in a moving range. A stack and a deque answer them in one pass.

1 of 8