Dashboard
LC #155

Min Stack

Pattern · ?

Problem

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

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, -2
Why
Stack state restores automatically on pop.

Spot the pattern

Which pattern cracks it?

Hints

One at a time
Solve on LeetCode ↗