easyStackStack

Min Stack

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

Recognize the pattern

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

Brute force idea

A straightforward first read of Min Stack is this: 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.

Better approach

A calmer way to see Min Stack is this: 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.

Key invariant

The truth you want to protect throughout Min Stack is this: 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.

Watch out for

A common way to get lost in Min Stack is this: 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