forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin_stack.py
More file actions
51 lines (43 loc) · 1.02 KB
/
min_stack.py
File metadata and controls
51 lines (43 loc) · 1.02 KB
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
47
48
49
50
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.arr = []
def push(self, val: int) -> None:
curr_min = self.getMin()
if curr_min is None or val < curr_min:
curr_min = val
self.arr.append((val, curr_min))
def pop(self) -> None:
if len(self.arr) != 0:
self.arr.pop()
def top(self) -> int:
if len(self.arr) > 0:
return self.arr[-1][0]
else:
return None
def getMin(self) -> int:
if len(self.arr) > 0:
return self.arr[-1][1]
else:
return None
# Your MinStack object will be instantiated and called as such:
obj = MinStack()
obj.push(2147483646)
obj.push(2147483646)
obj.push(2147483647)
print(obj.top())
obj.pop()
print(obj.getMin())
obj.pop()
print(obj.getMin())
obj.pop()
obj.push(2147483647)
print(obj.top())
print(obj.getMin())
obj.push(-2147483648)
print(obj.top())
print(obj.getMin())
obj.pop()
print(obj.getMin())