Home
DSA Patterns
Stack
Min Stack
Min Stack (LeetCode 155)
Min Stack (LeetCode 155): design a stack that supports retrieving the minimum element in constant time.
Problem Understanding
Design a stack that supports push, pop, top, and getMin operations, all in O(1) time.
push(val): Pushes element val onto stack.pop(): Removes element on top of stack.top(): Gets the top element.getMin(): Retrieves minimum element in the stack.
Algorithm Strategy
A standard stack gives O(1) push/pop, but finding the minimum usually takes O(N). To get O(1) min, we trade Space for Time.
Strategy:
Maintain a second stack, minStack. Whenever we push a value x onto the mainStack, we also push min(x, minStack.top()) onto the minStack. The top of minStack always represents the minimum of the mainStack at that height.
Interactive Visualization
EVERY OPERATION O(1)
EMPTY
operations
stack
empty
minStack
empty
Size
0
Min
:
returned
Nothing read yet
Two empty stacks: stack holds the values, minStack the minimum at each height
Next
push 2: add it to stack
Ops
Visualize two stacks growing in parallel. The MinStack tracks the minimum value seen 'so far'.
Dry Run
Ops: push 2, push 0, push 3, getMin, pop, top, getMin
| Op | stack | minStack | Returns |
|---|---|---|---|
| push 2 | [2] |
[2] |
- |
| push 0 | [2, 0] |
[2, 0] |
- |
| push 3 | [2, 0, 3] |
[2, 0, 0] (min(3, 0) = 0) |
- |
| getMin | [2, 0, 3] |
[2, 0, 0] |
0 |
| pop | [2, 0] |
[2, 0] |
- |
| top | [2, 0] |
[2, 0] |
0 |
| getMin | [2, 0] |
[2, 0] |
0 |
Edge Cases & Common Mistakes
- Empty Stack:
getMinon empty stack (usually invalid op by problem statement, but good to check). - Duplicate Min: If we push
0then another0, minStack should store both so when one pops, the other remains.
Solution
Using two stacks.
Min Stack Code
1var MinStack = function() {2 this.stack = [];3 this.minStack = [];4};56MinStack.prototype.push = function(val) {7 this.stack.push(val);8 this.minStack.push(this.minStack.length === 0 ? val : Math.min(val, this.minStack[this.minStack.length - 1]));9};1011MinStack.prototype.pop = function() {12 this.stack.pop();13 this.minStack.pop();14};1516MinStack.prototype.top = function() {17 return this.stack[this.stack.length - 1];18};1920MinStack.prototype.getMin = function() {21 return this.minStack[this.minStack.length - 1];22};
Min Stack in Python, Java, C++, TypeScript and C
Min Stack in Python
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
self.min_stack.append(min(val, self.min_stack[-1]) if self.min_stack else val)
def pop(self) -> None:
self.stack.pop()
self.min_stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]Min Stack in Java
class MinStack {
private final Deque<Integer> stack = new ArrayDeque<>();
private final Deque<Integer> minStack = new ArrayDeque<>();
public void push(int val) {
stack.push(val);
minStack.push(minStack.isEmpty() ? val : Math.min(val, minStack.peek()));
}
public void pop() {
stack.pop();
minStack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}Min Stack in C++
class MinStack {
stack<int> st;
stack<int> minSt;
public:
void push(int val) {
st.push(val);
minSt.push(minSt.empty() ? val : min(val, minSt.top()));
}
void pop() {
st.pop();
minSt.pop();
}
int top() {
return st.top();
}
int getMin() {
return minSt.top();
}
};Min Stack in TypeScript
class MinStack {
private stack: number[] = [];
private minStack: number[] = [];
push(val: number): void {
this.stack.push(val);
this.minStack.push(this.minStack.length === 0 ? val : Math.min(val, this.minStack[this.minStack.length - 1]));
}
pop(): void {
this.stack.pop();
this.minStack.pop();
}
top(): number {
return this.stack[this.stack.length - 1];
}
getMin(): number {
return this.minStack[this.minStack.length - 1];
}
}Min Stack in C
typedef struct {
int stack[30000];
int minStack[30000];
int size;
} MinStack;
MinStack* minStackCreate() {
MinStack* obj = malloc(sizeof(MinStack));
obj->size = 0;
return obj;
}
void minStackPush(MinStack* obj, int val) {
obj->stack[obj->size] = val;
obj->minStack[obj->size] = obj->size == 0 ? val : (val < obj->minStack[obj->size - 1] ? val : obj->minStack[obj->size - 1]);
obj->size++;
}
void minStackPop(MinStack* obj) {
obj->size--;
}
int minStackTop(MinStack* obj) {
return obj->stack[obj->size - 1];
}
int minStackGetMin(MinStack* obj) {
return obj->minStack[obj->size - 1];
}
void minStackFree(MinStack* obj) {
free(obj);
}More practice: DSA interview questions with answers · patterns cheat sheet
