-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_MinStack.py
More file actions
31 lines (22 loc) · 766 Bytes
/
03_MinStack.py
File metadata and controls
31 lines (22 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Question link - https://leetcode.com/problems/min-stack/description/?envType=study-plan-v2&envId=top-interview-150
class MinStack:
def __init__(self):
self.stack = []
self.minStack = []
def push(self, val: int) -> None:
self.stack.append(val)
minVal = min(val , self.minStack[-1] if self.minStack else val)
self.minStack.append(minVal)
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]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()