forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin-stack.py
More file actions
33 lines (21 loc) · 696 Bytes
/
min-stack.py
File metadata and controls
33 lines (21 loc) · 696 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
#https://leetcode.com/problems/min-stack/
class MinStack(object):
def __init__(self):
self.stack = []
def push(self, x):
self.stack.append((x, min(x, self.getMin())))
def pop(self):
if len(self.stack)==0: return None
return self.stack.pop()[0]
def top(self):
if len(self.stack)==0: return None
return self.stack[-1][0]
def getMin(self):
if len(self.stack)==0: return float('inf')
return self.stack[-1][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.getMin()