-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path12_min_stack.py
More file actions
54 lines (42 loc) · 963 Bytes
/
12_min_stack.py
File metadata and controls
54 lines (42 loc) · 963 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class MinStack:
def __init__(self):
self.stack = []
self.mins = []
def push(self, x):
"""
:type x: int
:rtype: void
"""
self.stack.append(x)
if not self.mins or x <= self.mins[-1]:
self.mins.append(x)
def pop(self):
"""
:rtype: int
"""
if not self.stack:
return -1
x = self.stack.pop()
if self.mins and x == self.mins[-1]:
self.mins.pop()
return x
def top(self):
"""
:rtype: int
"""
if not self.stack:
return -1
return self.stack[-1]
def min(self):
"""
:rtype: int
"""
if not self.mins:
return -1
return self.mins[-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.min()