Min Stack
Build a stack that can tell you its smallest value at any moment, in constant time.
Design a stack of integers with four operations. push(val)pop()top()getMin()poptopgetMin
Example 1
Output: getMin() = -3, top() = 0, getMin() = -2
The Idea
A stack works like a pile of plates. You put a new plate on top and take plates off the top, and the plates underneath are never touched. Pushing adds to the top, popping removes the top, and top()
Follow the example. After push(-2)push(0)push(-3)
Then pop()getMin()
Think about what a pop can change. It only removes the top, so while an entry stays on the stack, the entries under it stay exactly the same. That means the smallest value among an entry and everything under it, call it the entry's smallest so far, never changes while the entry is there. We can work it out once, when the entry is pushed, and store it next to the value.
Look at the picture below. Each row is one entry, with its value on the left and its smallest so far on the right.
In the stack labelled "after push(-3)", the bottom entry -2 has smallest so far -2, since nothing is under it. The entry 0 also has -2, because -2 is under it. The top entry -3 has -3, and getMin()
Now follow the pop()
Solution Approach
Turning that idea into code means settling three things: what each entry holds, how pushpoptopgetMin
What each entry holds
What does one entry need to carry? Two numbers: the value and its smallest so far. A small array int[] {val, min}ArrayDequejava.utilpushpoppeek
Working out the smallest so far on push
How does push
On an empty stack there is no entry underneath, so the smallest so far is the value itself. A value pushed twice is no special case either: each copy gets its own entry and its own smallest so far, so popping one copy leaves the other with the same minimum.
Reading and removing the top
What is left for the other three operations? Each one touches only the top entry. top()getMin()pop()
Java Solution
The class has five parts, one for the storage and one for each operation:
: anstack of entries, each anArrayDeque holding the value and its smallest so far.int[] : works outpush(val) , the new entry's smallest so far, frommin and the top entry'sval , then pushespeek()[1] .{val, min} : removes the top entry.pop() : returns the top entry's value,top() .peek()[0] : returns the top entry's smallest so far,getMin() .peek()[1]
The main
import java.util.ArrayDeque;
import java.util.Deque;
public class MinStack {
// each entry: {value, smallest value at or below this entry}
private final Deque<int[]> stack = new ArrayDeque<>();
public void push(int val) {
int min = stack.isEmpty() ? val : Math.min(val, stack.peek()[1]);
stack.push(new int[] {val, min});
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek()[0];
}
public int getMin() {
return stack.peek()[1];
}
public static void main(String[] args) {
MinStack s = new MinStack();
s.push(-2);
s.push(0);
s.push(-3);
System.out.println("getMin() = " + s.getMin());
s.pop();
System.out.println("top() = " + s.top() + ", getMin() = " + s.getMin());
MinStack d = new MinStack(); // the smallest value pushed twice
d.push(1);
d.push(1);
d.pop();
System.out.println("after push(1), push(1), pop(): getMin() = " + d.getMin());
MinStack u = new MinStack(); // a bigger value on top of the minimum
u.push(3);
u.push(7);
System.out.println("after push(3), push(7): top() = " + u.top()
+ ", getMin() = " + u.getMin());
u.pop();
u.pop();
u.push(9);
System.out.println("after both pops and push(9): getMin() = " + u.getMin());
}
}
Running it prints:
getMin() = -3
top() = 0, getMin() = -2
after push(1), push(1), pop(): getMin() = 1
after push(3), push(7): top() = 7, getMin() = 3
after both pops and push(9): getMin() = 9
The example's answers, -3, then 0 and -2, appear in the opening two lines. Look at top() = 0, getMin() = -2pop()
In the third line, one copy of 1 was popped, and getMin()getMin()push(9)
The original implementation,
Complexity
Every operation looks only at the top entry, so none of them gets slower as the stack grows.
- Time: O(1) for every operation, because
makes one comparison with the top's smallest so far and adds one entry, andpush ,pop andtop each touch only the top entry. Now and thengetMin copies its entries into a bigger array, but spread over all the pushes that still costs a constant amount per push.ArrayDeque - Space: O(n) for n values on the stack, because each value is stored together with its smallest so far: two numbers per value instead of one, which still grows in step with n.
A stack that can also remove its largest value from wherever it sits, not just read the smallest, takes more work; see