Min Stack
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
arg 1 = ["MinStack","push","push","push","getMin","pop","getMin","top"]
arg 2 = [[],[3],[1],[1],[],[],[],[]][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
arg 1 = ["MinStack","push","top","getMin"]
arg 2 = [[],[-8],[],[]][null,null,-8,-8]Explanation
A single negative value is simultaneously top and minimum.
Example 3
arg 1 = ["MinStack","push","push","getMin","pop","getMin","top"]
arg 2 = [[],[4],[-2],[],[],[],[]][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 contract
MinStackArguments: json, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
["MinStack","push","push","push","getMin","pop","getMin","top"][[],[3],[1],[1],[],[],[],[]]