16 · Stacks & Monotonic Stack

#155 Min Stack

Mediumsolve on LeetCode ↗

frequently reported atBloombergOracleAmazonMicrosoftGoogleMeta

the problem

In plain words

Design a stack with push(val), pop(), top() and getMin(). getMin() returns the smallest element currently in the stack. Every operation must run in O(1) time.

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

out -3, 0, -2

After popping -3 the minimum goes back to -2.

  • −2³¹ ≤ val ≤ 2³¹ − 1
  • pop, top and getMin are only called on a non-empty stack
  • At most 3·10⁴ calls

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #232
    2. #622
      Design Circular Queue
      Mediumin Queues
    3. #146
      LRU Cache
      Mediumin Linked Lists