Stack
LC #155Medium
Min Stack
Stack
Problem
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
stackdesign
Constraints
- ›-2³¹ ≤ val ≤ 2³¹ - 1
- ›At most 3 * 10⁴ calls will be made to push, pop, top, and getMin.
- ›Methods pop, top and getMin operations will always be called on non-empty stacks.
Example
Input
push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()Output
null, null, null, -3, null, 0, -2Why
Stack state restores automatically on pop.