Stack
Stack problems are about remembering the most recent unresolved item. The stack top is the only candidate that can be resolved next, which makes the pattern natural for nested structures, matching brackets, expression evaluation, and next-greater/next-smaller scans.
Quick Identifier
- Topic: stack
- Pattern: Stack, monotonic stack, two-stack simulation, or parser stack.
- Core Question: Does the newest unresolved item need to be handled before older items?
Quick Sights
- Typical Time Complexity:
O(n)because each item is pushed and popped at most once. - Typical Space Complexity:
O(n)in the worst case for unresolved items. - Core Mechanism: Push unresolved state; pop when the current token resolves the stack top.
Diagram
Loading diagram…
Recognition Cues
- Parentheses, brackets, nested expressions, or path segments.
- "Next greater/smaller", "previous warmer", or histogram boundaries.
- Need to simulate another data structure using stacks.
Problem List
Start with 020. Valid Parentheses, then move through 155. Min Stack, 150. Evaluate Reverse Polish Notation, 739. Daily Temperatures, and 084. Largest Rectangle in Histogram.
Last updated on
Spotted something unclear or wrong on this page?