-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path155.py
48 lines (37 loc) · 891 Bytes
/
155.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
# https://leetcode.com/problems/min-stack/
# seems using namedtuple from collections to present a node is slower than two lists
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.arr = []
self.mins = []
self.min = None
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.arr.append(x)
self.mins.append(self.min)
if self.min == None:
self.min = x
elif self.min > x:
self.min = x
def pop(self):
"""
:rtype: nothing
"""
self.arr.pop()
self.min = self.mins.pop()
def top(self):
"""
:rtype: int
"""
return self.arr[-1]
def getMin(self):
"""
:rtype: int
"""
return self.min