LeetCode ยท Stack
Stack Problem Set
LIFO problems โ parentheses matching, monotonic stacks, expression evaluation. When you need to "remember the last thing" or find the next greater/smaller element.
Concept page: Stack
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | matching | LC 20 ยท Valid Parentheses | Push openers, pop and match closers. O(n). |
| Medium | design | LC 155 ยท Min Stack | Two stacks: main + min tracking. O(1) getMin. |
| Medium | evaluation | LC 150 ยท Evaluate Reverse Polish Notation | Push numbers, pop two on operator, push result. O(n). |
| Medium | generation | LC 22 ยท Generate Parentheses | Backtracking with open/close counts. O(4โฟ/โn). |
| Medium | nested | LC 394 ยท Decode String | Stack of (string, count) pairs for nested encoding. O(n). |
| Medium | monotonic | LC 739 ยท Daily Temperatures | Monotonic decreasing stack. Pop when current > top. O(n). |
| Medium | monotonic | LC 853 ยท Car Fleet | Sort by position desc, stack tracks arrival times. O(n log n). |
| Hard | monotonic | LC 84 ยท Largest Rectangle in Histogram | Monotonic increasing stack. Calculate area on pop. O(n). |
| Medium | evaluation | LC 227 ยท Basic Calculator II | Single pass + stack. Collapse * and / immediately; defer + and - as signed terms. O(n). |