Min Stack
How to get the minimum value from a stack?
This is an interesting problem that looks simple at first. The basic stack operations are easy, but the challenge is how to get the minimum value quickly, in constant time.
Understanding the Problem
We are required to design a stack that supports push, pop, top, and the most challenging operation — getMin.
The operations push, which adds a value to the stack; pop, which removes the last value; and top, which retrieves the last value added (i.e., the top of the stack), can all be implemented with constant time complexity — O(1).
Get Alan Martins’s stories in your inbox
Join Medium for free to get updates from this writer.
Here’s how they work using a simple array-based stack in Python:
stack.append(val) # Adds a value to the end of the array (top of the stack)
stack.pop() # Removes the last value added (top of the stack)
stack[-1] # Retrieves the last value without removing itBut what about getMin?
How can we retrieve the minimum value in the stack while maintaining constant time complexity — O(1)?
How the Algorithm Works
The problem can be broken into simple steps:
- Create an empty array
stackto store the values. - For the operations
push,pop, andtop, the behavior is straightforward:push: add a value to the end of the stack.pop: remove the last value from the stack.top: return the last value in the stack. - The real challenge is
getMin. There are two ways to approach it:
- A simple (but inefficient) way is to use Python’s built-inmin()function to scan the entire stack and return the smallest value. This works but has linear time complexity — O(n).
- A more efficient way is to store the minimum value at each step. For everypush, along with storing the value, we also store the minimum value so far. This way,getMincan retrieve the current minimum instantly in constant time — O(1). - With this idea, every time we
pop, we remove both the value and its corresponding minimum at that point in time. - Both
topandgetMinsimply return the last stored values — one for the actual stack and one for the minimum tracker.
Brute-force solution:
Step 1: Create the stack
- Create an empty array
stackto store the values.
stack = []Step 2: Implement basic operations
push: Add a value to the end of the stack.
stack.append(val)pop: Remove the last value from the stack.
stack.pop()top: Return the last value without removing it.
stack[-1]Step 3: Get the minimum value
- Use Python’s built-in
min()function to scan the entire stack and return the smallest value.
min(stack)This solution works, but
getMin()has O(n) time complexity since it needs to scan the whole stack. The optimal approach improves that.
Full solution
class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
self.stack.append(val)
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return min(self.stack)Optimal solution
Step 1: Initialize the stack and the min tracker
- Create two stacks:
stackfor storing the values.min_stackfor tracking the minimum value at each step.
stack = []
min_stack = []Step 2: Implement the push operation
- Push the value to
stack. - For
min_stack: - If it’s empty or the new value is less than or equal to the current minimum, push the new value.
- Otherwise, repeat the current minimum at the top of
min_stack.
stack.append(val)
if not min_stack or val <= min_stack[-1]:
min_stack.append(val)
else:
min_stack.append(min_stack[-1])Step 3: Implement the pop operation
- Remove the last value from both
stackandmin_stack.
stack.pop()
min_stack.pop()Step 4: Implement top and getMin
top: Return the last value fromstack.
stack[-1]getMin: Return the last value frommin_stack— this is the minimum value at the current state.
min_stack[-1]This solution has O(1) time complexity for all operations, including
getMin(), and uses extra space to maintain the minimums.
Full solution
class MinStack:
def __init__(self):
self.stack = []
self.min_stack = []
def push(self, val: int) -> None:
self.stack.append(val)
if not self.min_stack or val <= self.min_stack[-1]:
self.min_stack.append(val)
else:
self.min_stack.append(self.min_stack[-1])
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]





