📚 PracticeMediumAlgorithm ProblemCoding Ready
Min Stack
stackdesign
LeetCode #155
Updated Dec 20, 2025
Question
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
LeetCode: Min Stack
Implement the MinStack class:
MinStack()initializes the stack objectvoid push(int val)pushes the element val onto the stackvoid pop()removes the element on the top of the stackint top()gets the top element of the stackint getMin()retrieves the minimum element in the stack
All operations must run in O(1) time.
Example:
Input:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
Output:
[null,null,null,null,-3,null,0,-2]
Explanation:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // return -3
minStack.pop();
minStack.top(); // return 0
minStack.getMin(); // return -2
Hints
Hint 1
You need to track the minimum at each level of the stack. Think about using an additional data structure.
Hint 2
Use two stacks: one for regular values, one to track minimums. The min stack always has the current minimum at its top.
Hint 3
When pushing, only add to min_stack if the new value is less than or equal to the current minimum. When popping, also pop from min_stack if it matches.
Your Solution
python
Auto-saves every 30s
Try solving the problem first before viewing the solution
Learning Resources
Videos
Related Problems
- •Max Stack (Similar problem)
- •Sliding Window Maximum- uses similar min tracking idea(#239)
0:00time spent