Problem
Section titled “Problem”Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
Implement the MinStack class:
MinStack()initializes the stack object.void push(int val)pushes the elementvalonto the stack.void pop()removes the element on the top of the stack.int top()gets the top element of the stack.int getMin()retrieves the minimum element in the stack.
You must implement a solution with time complexity for each function.
Example 1:
Input["MinStack","push","push","push","getMin","pop","top","getMin"][[],[-2],[0],[-3],[],[],[],[]]
Output[null,null,null,null,-3,null,0,-2]
ExplanationMinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.getMin(); // return -3minStack.pop();minStack.top(); // return 0minStack.getMin(); // return -2Constraints:
- Methods
pop,topandgetMinoperations will always be called on non-empty stacks. - At most calls will be made to
push,pop,top, andgetMin.
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”A stack only changes at its top, so the minimum of the stack can only change on a push or a pop. If we remember, for the current stack contents, “what was the minimum when this element became the minimum”, we can restore the previous minimum the moment that element is popped.
Keep a second stack mins alongside the main stack st. Invariant: mins.top() is the minimum of all elements currently in st, and mins holds, in order, every value that was a new minimum (ties included) at the moment it was pushed and is still in st.
- When
valis pushed andval <= mins.top(), it becomes the new minimum, so it goes onminstoo. - When the popped value equals
mins.top(), that copy of the minimum leaves the stack, so it leavesminstoo, and the minimum before it reappears on top.
Ties must be pushed (<=, not <): with two equal minimums in st, popping one must leave the other on mins.
Algorithm
Section titled “Algorithm”push(val): pushvalontost; ifminsis empty orval <= mins.top(), push it ontomins.pop(): ifst.top() == mins.top(), popmins; then popst.top()returnsst.top();getMin()returnsmins.top().
Walkthrough
Section titled “Walkthrough”Example 1:
| Operation | st (bottom → top) | mins (bottom → top) | Returns |
|---|---|---|---|
push(-2) | -2 | -2 | |
push(0) | -2, 0 | -2 | |
push(-3) | -2, 0, -3 | -2, -3 | |
getMin() | -2, 0, -3 | -2, -3 | -3 |
pop() | -2, 0 | -2 | |
top() | -2, 0 | -2 | 0 |
getMin() | -2, 0 | -2 | -2 |
Complexity
Section titled “Complexity”- Time: for every operation — each does a constant number of stack operations.
- Space: —
stholds allnelements andminsat mostn(only when values arrive in non-increasing order).
Edge cases
Section titled “Edge cases”- Duplicate minimums:
push(0), push(0), pop()leaves one0onminsthanks to<=, sogetMin()still returns0. - Extreme values: values are stored as
intand only compared, so and cause no overflow (unlike the “store2 * val - min” trick, which needslong long).
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class MinStack { public: void push(int val) { st.push(val); if (mins.empty() || val <= mins.top()) mins.push(val); }
void pop() { if (st.top() == mins.top()) mins.pop(); st.pop(); }
int top() { return st.top(); }
int getMin() { return mins.top(); }
private: stack<int> st; stack<int> mins; // mins.top() is the minimum of st};class MinStack { private final Deque<Integer> st = new ArrayDeque<>(); private final Deque<Integer> mins = new ArrayDeque<>(); // mins.peek() is the minimum of st
public MinStack() { }
public void push(int value) { st.push(value); if (mins.isEmpty() || value <= mins.peek()) mins.push(value); }
public void pop() { int x = st.pop(); if (x == mins.peek()) mins.pop(); }
public int top() { return st.peek(); }
public int getMin() { return mins.peek(); }}class MinStack:
def __init__(self): self.st = [] self.mins = [] # mins[-1] is the minimum of st
def push(self, value: int) -> None: self.st.append(value) if not self.mins or value <= self.mins[-1]: self.mins.append(value)
def pop(self) -> None: if self.st.pop() == self.mins[-1]: self.mins.pop()
def top(self) -> int: return self.st[-1]
def getMin(self) -> int: return self.mins[-1]Give it a try first
Attempt the problem on LeetCode — the code will still be here.