All Patterns

Stack

Use LIFO ordering to match pairs, track state, or maintain monotonic sequences for next-greater/smaller element problems.

When to use

  • Matching parentheses/brackets
  • Next greater/smaller element
  • Expression evaluation
  • Monotonic relationships

When NOT to use

  • Need random access
  • Need FIFO ordering (use queue)

Common traps

  • Empty stack access
  • Not handling the remaining elements after iteration
  • Confusing push/pop order

Key Invariant

The stack maintains elements in a useful order — monotonic stacks keep elements sorted for O(n) lookups

Problems (23)

#1Valid Parentheses
easyFREE
#52Min Stack
easy
#53Evaluate Reverse Polish Notation
easy
#54Daily Temperatures
easy
#55Next Greater Element I
easy
#56Implement Queue using Stacks
easy
#197Implement Stack using Queues
easy
#185Online Stock Span
medium
#186Remove K Digits
medium
#187132 Pattern
medium
#188Sum of Subarray Minimums
medium
#189Asteroid Collision
medium
#198Flatten Nested List Iterator
medium
#216Minimum Remove to Make Valid Parentheses
medium
#217Decode String
medium
#255Basic Calculator II
medium
#256Simplify Path
medium
#257Remove All Adjacent Duplicates in String II
medium
#258Exclusive Time of Functions
medium
#308Shortest Unsorted Continuous Subarray
medium
#118Largest Rectangle in Histogram
hard
#254Basic Calculator
hard
#288Maximal Rectangle
hard