easyStackStack

Min Stack

easyTime: O(1)Space: O(n)

Signals to notice

support push/pop/top/getMin in O(1)track minimum across operationsauxiliary state

Brute force first

Scan the entire stack for minimum on each getMin call. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

The key insight

Use a second stack that tracks the current minimum at each level — for all operations. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

What must stay true

The min stack always has the same height as the main stack; its top is the current minimum. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Easy way to go wrong

Only pushing to min stack when a new minimum is found — push on every operation to keep stacks synchronized. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Stack Pattern