- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathday31_155.py
46 lines (37 loc) · 1010 Bytes
/
day31_155.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
fromtypingimportList, Tuple
classMinStack:
"""
stack
(-3, -3)
(0, -2)
(-2, -2)
"""
def__init__(self):
self.stack: List[Tuple[int, int]] = []
defpush(self, val: int) ->None:
iflen(self.stack) ==0:
self.stack.append((val, val))
else:
cur_mins=self.stack[-1][1]
ifval<cur_mins:
self.stack.append((val, val))
else:
self.stack.append((val, cur_mins))
defpop(self) ->None:
self.stack=self.stack[:-1]
deftop(self) ->int:
ifself.stack:
returnself.stack[-1][0]
defgetMin(self) ->int:
ifself.stack:
returnself.stack[-1][1]
if__name__=="__main__":
# Your MinStack object will be instantiated and called as such:
obj=MinStack()
obj.push(-2)
obj.push(0)
obj.push(-3)
assertobj.getMin() ==-3
obj.pop()
assertobj.top() ==0
assertobj.getMin() ==-2