Skip to content
BASA DSA

Min Stack

MediumStack

Implement MinStack with push(val), pop(), top() and getMin(). top and getMin return the current top and minimum. All operations should take constant time; pop returns nothing.

Example 1

Input
arg 1 = ["MinStack","push","push","push","getMin","pop","getMin","top"]
arg 2 = [[],[3],[1],[1],[],[],[],[]]
Output
[null,null,null,null,1,null,1,1]

Explanation

Popping one of the two 1 values leaves another 1 as both top and minimum.

Example 2

Input
arg 1 = ["MinStack","push","top","getMin"]
arg 2 = [[],[-8],[],[]]
Output
[null,null,-8,-8]

Explanation

A single negative value is simultaneously top and minimum.

Example 3

Input
arg 1 = ["MinStack","push","push","getMin","pop","getMin","top"]
arg 2 = [[],[4],[-2],[],[],[],[]]
Output
[null,null,null,-2,null,4,4]

Explanation

Popping -2 restores the previous minimum 4 instead of retaining a removed minimum.

Constraints

  • At most 30000 operations
  • Queries and pop occur only on nonempty stacks
  • Values are signed 32-bit integers

Class contractMinStackArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…