Problem Number: 155 Difficulty: Medium Category: Stack, Design LeetCode Link: https://leetcode.com/problems/min-stack/
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 O(1) 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]
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
Constraints:
-2^31 <= val <= 2^31 - 1- Methods
pop,topandgetMinoperations will always be called on non-empty stacks. - At most
3 * 10^4calls will be made topush,pop,top, andgetMin.
I used a Two Stack approach to maintain both the main stack and a minimum stack. The key insight is to use a separate stack to track the minimum value at each state.
Algorithm:
- Use two deques: one for main stack, one for minimum values
- Push: Add value to main stack, add minimum of current min and new value to min stack
- Pop: Remove from both stacks simultaneously
- Top: Return top element of main stack
- GetMin: Return top element of min stack
The solution uses two stacks to maintain O(1) time complexity for all operations. See the implementation in the solution file.
Key Points:
- Uses two deques for main stack and minimum tracking
- Maintains minimum value at each stack state
- All operations are O(1) time complexity
- Handles duplicate minimum values correctly
- Synchronized operations between both stacks
Time Complexity: O(1) for all operations
- Push: O(1) - append to both stacks
- Pop: O(1) - remove from both stacks
- Top: O(1) - access last element
- GetMin: O(1) - access last element of min stack
Space Complexity: O(n)
- Main stack: O(n)
- Min stack: O(n)
- Total: O(n)
-
Two Stack Design: Using two stacks allows O(1) minimum retrieval.
-
Minimum Tracking: The min stack tracks the minimum value at each state.
-
Synchronized Operations: Push and pop operations affect both stacks simultaneously.
-
Duplicate Handling: The min stack can contain duplicate values for consecutive minimums.
-
Constant Time: All operations maintain O(1) time complexity.
-
Stack Property: The min stack naturally handles the LIFO property of the main stack.
-
Single Stack: Initially might try to use a single stack with complex logic.
-
Linear Search: Using linear search to find minimum, leading to O(n) complexity.
-
Complex Implementation: Overcomplicating the minimum tracking logic.
-
Wrong Data Structure: Not using appropriate data structures for O(1) operations.
- Max Stack (Problem 716): Design stack with max operation
- Valid Parentheses (Problem 20): Stack-based parentheses validation
- Evaluate Reverse Polish Notation (Problem 150): Stack-based expression evaluation
- Implement Queue using Stacks (Problem 232): Queue implementation with stacks
- Single Stack with Pairs: Store (value, min) pairs in single stack - O(1) time, O(n) space
- Variable Tracking: Track minimum with variable and handle updates - O(1) average time
- Linked List: Use linked list with minimum tracking - O(1) time, O(n) space
- Single Stack Attempt: Trying to use single stack with complex minimum tracking.
- Linear Search: Using linear search to find minimum in O(n) time.
- Complex Logic: Overcomplicating the minimum tracking implementation.
- Wrong Data Structure: Not using appropriate data structures for O(1) operations.
- Synchronization Issues: Not properly synchronizing operations between stacks.
Note: This is a design problem that demonstrates efficient stack implementation with constant time minimum retrieval.