-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0155-min-stack.py
More file actions
46 lines (34 loc) · 874 Bytes
/
0155-min-stack.py
File metadata and controls
46 lines (34 loc) · 874 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
40
41
42
43
44
from typing import List
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.minStack = []
def push(self, x: int) -> None:
self.stack.append(x)
if self.minStack == []:
self.minStack.append(x)
else:
if x <= self.minStack[-1]:
self.minStack.append(x)
else:
self.minStack.append(self.minStack[-1])
def pop(self) -> None:
self.stack.pop()
self.minStack.pop()
def top(self) -> int:
return self.stack[-1]
def getMin(self) -> int:
return self.minStack[-1]
def main():
obj = MinStack()
obj.push(-2)
obj.push(0)
obj.push(-3)
obj.pop()
print(obj.top())
print(obj.getMin())
if __name__ == "__main__":
main()