-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmin-stack.py
More file actions
34 lines (28 loc) · 822 Bytes
/
min-stack.py
File metadata and controls
34 lines (28 loc) · 822 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
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self._stack = []
self._min_stack = []
def push(self, x: 'int') -> 'None':
self._stack.append(x)
if len(self._min_stack):
if x <= self._min_stack[-1]:
self._min_stack.append(x)
else:
self._min_stack.append(x)
def pop(self) -> 'None':
top = self._stack.pop()
if top == self._min_stack[-1]:
self._min_stack.pop()
def top(self) -> 'int':
return self._stack[-1]
def getMin(self) -> 'int':
return self._min_stack[-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()