Min Stack
LeetCode 155 • Medium • Design
Design a stack with push, pop, top, and getMin in O(1).
Use auxiliary stack: push min when value ≤ current min. Pop both when min matches top.
Input: push(-2), push(0), push(-3), getMin=-3, pop(), top=0, getMin=-2
PushO(1)append
PopO(1)pop
GetMinO(1)min stack top
SpaceO(n)
Step0/8
stack: (empty) minStack: (empty)
Ready
Press Play or Step. Two stacks: main stack + min stack. Push min when val ≤ current min; pop both when min matches top.