Stacks and monotonic stacks
Remember unresolved candidates.
A stack stores exactly what later work must revisit in reverse order: unmatched structure, child values, saved aggregates, unresolved indices, or surviving objects.
Mechanism and state
Choose the meaning of one entry before choosing push/pop rules. Bracket stacks hold unmatched opening types; postfix stacks hold completed child values; Min Stack nodes save suffix minima; path stacks hold canonical names. Next-greater stacks retain unresolved indices and resolve only on a strict greater encounter. Histogram candidates use a different order and equality rule: popping equal heights transfers future width ownership. Asteroid stacks hold the stable spatial survivors of a processed prefix.
Why the pattern works
State the invariant at the point where each step completes. LIFO matching proves nesting and postfix operand order; saved per-node aggregates prove minimum restoration; a stable survivor prefix proves collision locality. For monotonic algorithms, explain both why a popped answer/span is valid and why removing that entry loses no future optimum. Each index enters once and leaves at most once, so nested pops can total O(n). An equal histogram pop may be a partial span whose ownership transfers, not a finalized widest rectangle.
Work through an example
For ([{}]), the newest opening closes first; ([)] fails at its mismatched top. For postfix 7 -3 /, pop right=-3 then left=7 and obtain -2. A Min Stack containing 3,1,1 must still report one after removing either later minimum. In histogram [2,2,2], equal representatives transfer so the final one reaches width three. In [10,2,-5], the incoming asteroid destroys two before meeting ten.
Where the reasoning fails
Do not reuse one equality or direction rule across all stack tasks. Equal temperatures/values do not resolve strict next-greater queries, while equal histogram heights may transfer span ownership. Only a positive top and negative incoming asteroid approach at equal speed. Widen before negating Integer.MIN_VALUE, multiply rectangle area in long, preserve postfix operand order, and treat lexical paths separately from symbolic-link resolution. A fixed-window maximum needs a deque front for expiration, not only a stack back.
Reconstruct the reasoning
Trace stack contents bottom-to-top for ([)], 7 -3 /, duplicate minimum pushes/pops, [1,2,1] circular next greater, histogram [2,2,2], and the cascade [10,2,-5]. Identify exactly which steps finalize work, transfer ownership, restore old state, or reject input. Explain why /../../... stays at root before appending the literal ... name.
Apply this foundation
- Sliding Window Maximum · Explanation
- Valid Parentheses · Explanation
- Min Stack · Explanation
- Evaluate Reverse Polish Notation · Explanation
- Daily Temperatures · Explanation
- Next Greater Element — Circular Array · Explanation
- Largest Rectangle in Histogram · Explanation
- Asteroid Collision · Explanation
- Simplify Absolute Path · Explanation
- Kth Largest Element in an Array · Explanation