min_stack
1# @leet start 2class MinStack: 3 """ 4 This problem implements a min stack. 5 A min stack supports `push`, `pop`, `top`, and retrieving the minimum of a stack 6 in constant time. 7 To do so, for each item, the stack keeps the minimum value it has seen so far. 8 So each item in the stack is a tuple of (int, int), where the first item 9 is the minimum up to the current point, and the second item is the item itself. 10 This means that when adding to the stack, we need to check the stack top's minimum 11 and compare it with our current value. If the previous minimum is smaller, keep that. 12 If not, set the current minimum to the current value for the current item. 13 """ 14 15 def __init__(self): 16 self.stack = [] 17 18 def push(self, val: int) -> None: 19 if not self.stack: 20 self.stack.append((val, val)) 21 else: 22 prev_min = self.stack[-1][0] 23 self.stack.append((min(prev_min, val), val)) 24 25 def pop(self) -> None: 26 self.stack.pop() 27 28 def top(self) -> int: 29 return self.stack[-1][1] 30 31 def getMin(self) -> int: 32 return self.stack[-1][0] 33 34 35# Your MinStack object will be instantiated and called as such: 36# obj = MinStack() 37# obj.push(val) 38# obj.pop() 39# param_3 = obj.top() 40# param_4 = obj.getMin() 41# @leet end 42 43 44def test(): 45 assert 2 + 2 == 4
class
MinStack:
3class MinStack: 4 """ 5 This problem implements a min stack. 6 A min stack supports `push`, `pop`, `top`, and retrieving the minimum of a stack 7 in constant time. 8 To do so, for each item, the stack keeps the minimum value it has seen so far. 9 So each item in the stack is a tuple of (int, int), where the first item 10 is the minimum up to the current point, and the second item is the item itself. 11 This means that when adding to the stack, we need to check the stack top's minimum 12 and compare it with our current value. If the previous minimum is smaller, keep that. 13 If not, set the current minimum to the current value for the current item. 14 """ 15 16 def __init__(self): 17 self.stack = [] 18 19 def push(self, val: int) -> None: 20 if not self.stack: 21 self.stack.append((val, val)) 22 else: 23 prev_min = self.stack[-1][0] 24 self.stack.append((min(prev_min, val), val)) 25 26 def pop(self) -> None: 27 self.stack.pop() 28 29 def top(self) -> int: 30 return self.stack[-1][1] 31 32 def getMin(self) -> int: 33 return self.stack[-1][0]
This problem implements a min stack.
A min stack supports push, pop, top, and retrieving the minimum of a stack
in constant time.
To do so, for each item, the stack keeps the minimum value it has seen so far.
So each item in the stack is a tuple of (int, int), where the first item
is the minimum up to the current point, and the second item is the item itself.
This means that when adding to the stack, we need to check the stack top's minimum
and compare it with our current value. If the previous minimum is smaller, keep that.
If not, set the current minimum to the current value for the current item.
def
test():