Min Stack
Problem statement
Implement a stack with push, pop, top, and getMin. Each operation, including minimum lookup, must take O(1) time.
For this exercise, assume a fresh empty stack receives the ordered commands in operations. A push uses the matching entry in values; other commands ignore that entry. Return an array containing the answers to top and getMin, in command order. push and pop produce no output.
For this exercise, assume pop, top, and getMin are called only when the stack is nonempty.
Function
runMinStack(operations: String[], values: int[]) → int[]Examples
Example 1
operations = ["push","push","push","getMin","pop","top","getMin"]values = [-2,0,-3,0,0,0,0]return = [-3,0,-2]After the three pushes, the stack is [-2,0,-3], with minimum -3. Popping removes -3. The remaining top is 0 and minimum is -2.
Example 2
operations = ["push","push","pop","getMin"]values = [4,4,0,0]return = [4]Both pushes add 4. Removing one copy leaves the other, so the minimum remains 4.
Constraints
0 <= operations.length <= 20000values.length == operations.length- Every operation is
push,pop,top, orgetMin. pop,top, andgetMinare called only when the stack is nonempty.-10^9 <= values[i] <= 10^9