forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesign-minstack.py
More file actions
51 lines (40 loc) · 1 KB
/
Copy pathdesign-minstack.py
File metadata and controls
51 lines (40 loc) · 1 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
51
class MinStack(object):
def __init__(self):
self.stack = []
self.minstack = []
def push(self, val):
"""
:type val: int
:rtype: None
"""
self.stack.append(val)
#if minstack is empty
if not self.minstack:
self.minstack.append(val)
#if minstack is not empty
elif self.minstack[-1] < val:
self.minstack.append(self.minstack[-1])
else: # if val is less than min
self.minstack.append(val)
def pop(self):
"""
:rtype: None
"""
self.stack.pop()
self.minstack.pop()
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def getMin(self):
"""
:rtype: 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()