Problem Description
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.
Examples
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
Python 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)
def pop(self) -> None:
if self.stack[-1] == self.min_stack[-1]:
self.min_stack.pop()
self.stack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.min_stack[-1]
Java Solution
class MinStack {
private Stack stack;
private Stack minStack;
public MinStack() {
stack = new Stack<>();
minStack = new Stack<>();
}
public void push(int val) {
stack.push(val);
if (minStack.isEmpty() || val <= minStack.peek()) {
minStack.push(val);
}
}
public void pop() {
if (stack.peek().equals(minStack.peek())) {
minStack.pop();
}
stack.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return minStack.peek();
}
}
C++ Solution
class MinStack {
private:
stack stack;
stack minStack;
public:
MinStack() {
}
void push(int val) {
stack.push(val);
if (minStack.empty() || val <= minStack.top()) {
minStack.push(val);
}
}
void pop() {
if (stack.top() == minStack.top()) {
minStack.pop();
}
stack.pop();
}
int top() {
return stack.top();
}
int getMin() {
return minStack.top();
}
};
JavaScript Solution
/**
* initialize your data structure here.
*/
var MinStack = function() {
this.stack = [];
this.minStack = [];
};
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
this.stack.push(val);
if (this.minStack.length === 0 || val <= this.minStack[this.minStack.length - 1]) {
this.minStack.push(val);
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
if (this.stack[this.stack.length - 1] === this.minStack[this.minStack.length - 1]) {
this.minStack.pop();
}
this.stack.pop();
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.stack[this.stack.length - 1];
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.minStack[this.minStack.length - 1];
};
C# Solution
public class MinStack {
private Stack stack;
private Stack minStack;
public MinStack() {
stack = new Stack();
minStack = new Stack();
}
public void Push(int val) {
stack.Push(val);
if (minStack.Count == 0 || val <= minStack.Peek()) {
minStack.Push(val);
}
}
public void Pop() {
if (stack.Peek() == minStack.Peek()) {
minStack.Pop();
}
stack.Pop();
}
public int Top() {
return stack.Peek();
}
public int GetMin() {
return minStack.Peek();
}
}
Complexity Analysis
- Time Complexity: O(1) for all operations
- Space Complexity: O(n) where n is the number of elements in the stack
Solution Explanation
This solution uses two stacks to maintain minimum element:
- Key concept:
- Two stacks
- Minimum tracking
- Constant time
- Algorithm steps:
- Main stack operations
- Minimum stack updates
- Synchronize stacks
- Return results
Key points:
- Constant time operations
- Space trade-off
- Stack synchronization
- Minimum maintenance