-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmin_stack.py
More file actions
39 lines (27 loc) · 764 Bytes
/
min_stack.py
File metadata and controls
39 lines (27 loc) · 764 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
# 155. Min Stack
# https://leetcode.com/problems/min-stack/
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.arr = []
def push(self, x: int) -> None:
if len(self.arr) == 0:
self.arr.append((x, x))
else:
self.arr.append((x, min(self.getMin(), x)))
def pop(self) -> None:
self.arr.pop()
def top(self) -> int:
return self.arr[-1][0]
def getMin(self) -> int:
if len(self.arr) == 1:
return self.arr[0][1]
return self.arr[-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()