← DSADSA 1.5

Stack

Not started yet — 13 problems queued across four sub-patterns.

A LIFO structure, ideal for matching/parsing problems and "look back" problems. The monotonic stack keeps the stack increasing or decreasing, popping while the incoming element breaks the order — each pop is the answer for the popped index, which is how next-greater/next-smaller problems get solved in O(n) instead of O(n²).

Sub-patterns

  • A — Matching & Parsing. Bracket matching and calculator-style expression parsing.
  • B — Monotonic Stack. Next-greater/next-smaller in O(n) by popping while order is broken.
  • C — Stack-Based Simulation. Using a stack to directly model a process (encode/decode, collisions, path simplification).
  • D — Min-Stack Style Design. Augmenting a stack to answer an extra query (like current minimum) in O(1).

Practice set (13)

  • Easy — Valid Parentheses, Baseball Game
  • Medium — Min Stack, Daily Temperatures, Evaluate Reverse Polish Notation, Next Greater Element II, Decode String, Asteroid Collision, Online Stock Span, Remove K Digits, Simplify Path
  • Hard — Largest Rectangle in Histogram, Basic Calculator

Notes from readers

Comments — via GitHub