97. Min Stack

Easy · Stack

Implement a Min Stack that supports efficient minimum tracking. You'll receive an array of operations to perform on a stack. For each operation: push(N) adds N to the stack, pop() removes the top element, top() returns the top element without removing it, and getMin() returns the smallest element currently in the stack. Return an array containing all values returned by top() and getMin() operations, in the order they were called. Hint: you may need to track the minimum as you go rather than searching for it each time.

Examples

Example 1
Input: [["push",-2],["push",0],["push",-3],["getMin"],["pop"],["top"],["getMin"]]
Output: [-3, 0, -2]
Explanation: After pushes the min is -3; pop -3; top is now 0; min is -2
Example 2
Input: [["push",1],["push",2],["top"],["getMin"]]
Output: [2, 1]
Explanation: Top=2, min=1

Constraints