-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path155.min-stack.py
More file actions
57 lines (40 loc) · 1.24 KB
/
155.min-stack.py
File metadata and controls
57 lines (40 loc) · 1.24 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
from typing import List, Tuple
class MinStack_one:
def __init__(self):
self.stack = []
def push(self, val: int) -> None:
min_val = self.getMin()
if min_val is None or val <= min_val:
self.stack.append((val, val))
else:
self.stack.append((val, min_val))
def pop(self) -> None:
return self.stack.pop()
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int | None:
return self.stack[-1] if self.stack else None
# @leet start
class MinStack:
def __init__(self):
self.stack: List[Tuple[int, int]] = []
def push(self, val: int) -> None:
if self.stack:
min_val = min(val, self.stack[-1][1])
else:
min_val = val
self.stack.append((val, min_val))
def pop(self) -> None:
if self.stack:
self.stack.pop()
def top(self) -> int | None:
return self.stack[-1][0] if self.stack else None
def getMin(self) -> int | None:
return self.stack[-1][1] if self.stack else None
# 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()
# @leet end