Description

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class with these operations. Return the result as an array.

Examples

Input:["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]]
Output:[null,null,null,null,-3,null,0,-2]
Explanation:

After pushing -2, 0, and -3, getMin returns -3; after popping -3, top is 0 and getMin is -2, so the result is [null,null,null,null,-3,null,0,-2].

Input:input = ["push","push","getMin","pop","getMin"], arg2 = [-2,0,-2,0]
Output:[null,null,-2,null,-2]
Explanation:

After pushing -2 and 0, getMin returns -2; after popping 0, getMin is still -2, so the result is [null,null,-2,null,-2].

Input:operations = ["MinStack","push","push","push","top","getMin","pop","getMin","pop","top","getMin"], values = [[],[5],[1],[3],[],[],[],[],[],[],[]]
Output:[null,null,null,null,3,1,null,1,null,1,1]
Explanation:

The stack operations produce [null,null,null,null,3,1,null,1,null,1,1].

Constraints

  • -2³¹ ≤ val ≤ 2³¹ - 1
  • Methods pop, top and getMin will always be called on non-empty stacks.
  • At most 3 × 10⁴ calls will be made to push, pop, top, and getMin.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!