Min Stack

Stacks & Monotonic Stacks, problem 3 of 7

Min Stack

Medium

LC #155

stackdesign

Not attempted yet

Design a class MinStack that supports these operations, each in O(1) time:

  • MinStack() creates an empty stack.
  • push(val) puts val on top.
  • pop() removes the top element.
  • top() returns the top element.
  • get_min() returns the smallest element in the stack.

pop, top and get_min are only called on a non-empty stack.

Tests list the operations and their arguments; the output holds each call's return value (None for the constructor, push and pop).

Example 1

Input:
["MinStack","push","push","push",
 "get_min","pop","top","get_min"]
[[],[-2],[0],[-3],[],[],[],[]]
Output:
[None,None,None,None,-3,None,0,-2]

Example 2

Input:
["MinStack","push","top","get_min"]
[[],[5],[],[]]
Output: [None,None,5,5]

Example 3

Input:
["MinStack","push","push","get_min",
 "pop","get_min","push","get_min"]
[[],[3],[7],[],[],[],[1],[]]
Output:
[None,None,None,3,None,3,None,1]

Constraints

  • -2^31 <= val <= 2^31 - 1
  • at most 10^5 calls in total

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.