Min Stack
Question
Design a stack class that supports the push, pop, top, and getMin operations.
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.
Each function should run in O(1)O(1) time.
Example 1:
Input: ["MinStack", "push", 1, "push", 2, "push", 0, "getMin", "pop", "top", "getMin"]
Output: [null,null,null,null,0,null,2,1]
Explanation:
MinStack minStack = new MinStack();
minStack.push(1);
minStack.push(2);
minStack.push(0);
minStack.getMin(); // return 0
minStack.pop();
minStack.top(); // return 2
minStack.getMin(); // return 1
Constraints:
-2^31 <= val <= 2^31 - 1.pop,topandgetMinwill always be called on non-empty stacks.
Solution
Two Stack
Keep both the minStack has the min value up to the current item in the stack. If we pop we pop both
class MinStack:
def __init__(self):
self.minStack = []
self.stack = []
def push(self, val: int) -> None:
self.stack.append(val)
self.minStack.append(min(val, self.minStack[-1] if self.minStack else val))
def pop(self) -> None:
self.stack.pop()
self.minStack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1]
One stack
Group the (val, minValue) together
class MinStack:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
if not self.stack:
self.stack.append((val, val))
else:
_, prevMinValue = self.stack[-1]
self.stack.append((val, min(prevMinValue, val)))
def pop(self) -> None:
self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[-1][1]