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.
The problem
Three questions, one family:
- Is a string of brackets like
([]{})correctly nested? - For each element, where is the next element to its right that is larger?
- 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