-
Notifications
You must be signed in to change notification settings - Fork 0
/
155. Min Stack.py
51 lines (42 loc) · 1.04 KB
/
155. Min Stack.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
47
48
49
50
51
class MinStack(object):
def __init__(self):
self.stack = []
self.min_arr = []
self.min = 2**31
self.min_arr.append(self.min)
def push(self, val):
"""
:type val: int
:rtype: None
"""
if(val<=self.min):
self.min = val
self.min_arr.append(val)
self.stack.append(val)
def pop(self):
"""
:rtype: None
"""
top = self.stack[len(self.stack)-1]
if(top==self.min):
self.min_arr.pop()
self.min = self.min_arr[len(self.min_arr)-1]
return self.stack.pop()
def top(self):
"""
:rtype: int
"""
return self.stack[len(self.stack)-1]
def getMin(self):
"""
:rtype: int
"""
if not self.min_arr:
return 0
return self.min
# 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()