ASAPUtils Logo ASAPUtils
Week 3

Min Stack

Design a stack whose push, pop, top, and getMin operations are all O(1), with the per-depth minimum insight, C++ and JavaScript implementations, tradeoffs, and an interactive operation trace.

medium Stack (LIFO) Stack (LIFO) O(1) per operation time · O(n) space Open on LeetCode ↗

The problem

Design a stack that supports push, pop, top, and retrieving the current minimum, with every operation running in constant time.

push(-2), push(0), push(-3), getMin() -> -3, pop(), top() -> 0, getMin() -> -2

Related Problems