Description
Design a stack supporting push, pop, top, and getMin, all in constant time. Implement a versioned min stack.
Examples
Input:
operations = [["push",3],["push",1],["push",2],["getMin"],["pop"],["getMin"],["pop"],["getMin"]]Output:
[null,null,null,1,null,1,null,3]Explanation:
Min changes with pops.
Input:
operations = [[1]]Output:
[1]Explanation:
Minimal case with a single operation.
Input:
operations = [["push",-5],["getMin"],["push",-2],["push",-8],["getMin"],["pop"],["push",0],["getMin"],["pop"],["pop"],["getMin"]]Output:
[null,-5,null,null,-8,null,null,-5,null,null,-5]Explanation:
Stack operations with negative values: push -5 (min=-5), push -2 (min=-5), push -8 (min=-8), pop -8 (min back to -5), push 0 (min still -5), pop 0 then pop -2 (min still -5 from bottom element).
Constraints
- •
-2³¹ ≤ val ≤ 2³¹ - 1