algorythms
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

Inputpush(-2), push(0), push(-3), getMin(), pop(), top(), getMin()
Outputnull, null, null, -3, null, 0, -2
Why

Stack state restores automatically on pop.

Hints — reveal one at a time