medium

Min Stack

Design a stack supporting push, pop, top and getMin, where getMin returns the smallest element currently in the stack. Every operation must run in constant time.

Constraints
  • -2^31 ≤ val ≤ 2^31 - 1
  • At most 3 · 10^4 operations
  • pop, top, getMin are called only on a non-empty stack
Examples
in: push(-2), push(0), push(-3), getMin(), pop(), top(), getMin()
out: -3, 0, -2

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.