-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155_min_stack.py
More file actions
39 lines (31 loc) · 864 Bytes
/
155_min_stack.py
File metadata and controls
39 lines (31 loc) · 864 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
# https://leetcode.com/problems/min-stack/
class MinStack:
def __init__(self):
self.stack = []
self.min = []
def push(self, val: int) -> None:
if self.stack:
min = self.min[len(self.min)-1]
if val < min:
self.min.append(val)
else:
self.min.append(min)
else:
self.min.append(val)
self.stack.append(val)
def pop(self) -> None:
if self.stack:
self.stack.pop()
self.min.pop()
def top(self) -> int:
return self.stack[len(self.stack)-1]
def getMin(self) -> int:
return self.min[len(self.min)-1]
# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
# T = O(1)
# S = O(N)