Instructions
Write a class MinStack: a stack of integers that can also tell you its smallest value at any time.
The class has these methods:
MinStack()creates an empty stack.push(x)putsxon top of the stack. It returns nothing.pop()removes the top value. It returns nothing.top()returns the top value without removing it.getMin()returns the smallest value that is in the stack right now.
pop, top and getMin are only called when the stack is not empty.
Every method must run in O(1) time. Scanning the whole stack in getMin is too slow.
How the input is shown
The test gives two lists of the same length. operations[i] is the name of a call and arguments[i] is the list of values for that call. The first call is always "MinStack" (create the object). For example, operations = ["MinStack", "push", "getMin"] with arguments = [[], [6], []] means: create a stack, call push(6), then call getMin().
The expected output is a list with one entry per call: the value the call returns, or null for the constructor, push and pop, which return nothing.
Hint: when you push a value, also remember what the minimum is at that moment (for example, keep pairs [value, minimum so far]). When you pop, the minimum below is already stored.
Examples
Example 1
- Input:
- ["MinStack","push","push","push","getMin","pop","getMin","top"], [[],[5],[8],[2],[],[],[],[]]
- Output:
- [null,null,null,null,2,null,5,8]
The stack holds 5, 8, 2 (2 on top), so the minimum is 2. After pop removes 2, the stack holds 5, 8: the minimum is 5 and the top is 8.
Example 2
- Input:
- ["MinStack","push","push","push","pop","getMin","pop","getMin"], [[],[3],[1],[1],[],[],[],[]]
- Output:
- [null,null,null,null,null,1,null,3]
The value 1 is pushed twice. After the first pop, one 1 is still in the stack, so the minimum is still 1. After the second pop only 3 is left.
Example 3
- Input:
- ["MinStack","push","top","getMin","pop","push","push","getMin","top"], [[],[-4],[],[],[],[0],[-1],[],[]]
- Output:
- [null,null,-4,-4,null,null,null,-1,-1]
With one value, -4 is both the top and the minimum. The stack then becomes empty, and a new stack 0, -1 has minimum -1 and top -1.
Constraints
-20 <= x <= 20pop,topandgetMinare called only on a non-empty stack.- At most
30calls topush,pop,topandgetMinin total, after the constructor.
3 examples run when you press Run. Submit checks every hidden test too.
- Input:
- ["MinStack","push","push","push","getMin","pop","getMin","top"], [[],[5],[8],[2],[],[],[],[]]
- Expected:
- [null,null,null,null,2,null,5,8]
- Input:
- ["MinStack","push","push","push","pop","getMin","pop","getMin"], [[],[3],[1],[1],[],[],[],[]]
- Expected:
- [null,null,null,null,null,1,null,3]
- Input:
- ["MinStack","push","top","getMin","pop","push","push","getMin","top"], [[],[-4],[],[],[],[0],[-1],[],[]]
- Expected:
- [null,null,-4,-4,null,null,null,-1,-1]
Run checks your code against the 3 examples. Submit checks it against all 73 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.