-
Notifications
You must be signed in to change notification settings - Fork 481
/
0155.py
41 lines (36 loc) · 835 Bytes
/
0155.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
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.data = []
self.min_val = float("inf")
def push(self, x):
"""
:type x: int
:rtype: void
"""
if x <= self.min_val:
self.data.append(self.min_val)
self.min_val = x
self.data.append(x)
def pop(self):
"""
:rtype: void
"""
if self.data[-1] == self.min_val:
self.data.pop()
self.min_val = self.data[-1]
self.data.pop()
else:
self.data.pop()
def top(self):
"""
:rtype: int
"""
return self.data[-1]
def getMin(self):
"""
:rtype: int
"""
return self.min_val