-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN155最小栈.py
More file actions
39 lines (30 loc) · 898 Bytes
/
N155最小栈.py
File metadata and controls
39 lines (30 loc) · 898 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
import math
class MinStack:
def __init__(self):
self.min_index = 0
self.min_value = math.inf
self.stack = []
def push(self, val: int) -> None:
if val < self.min_value:
self.min_value = val
self.min_index = len(self.stack)
self.stack.append((val, self.min_index))
def pop(self) -> None:
self.stack.pop()
if not self.stack:
self.min_index = self.stack[-1][1]
self.min_value = self.stack[self.min_index][0]
else:
self.min_index = 0
self.min_value = math.inf
def top(self) -> int:
return self.stack[-1][0]
def getMin(self) -> int:
return self.stack[self.stack[-1][1]][0]
a = MinStack()
a.push(-10)
a.push(-10)
print(a.getMin(), a.getMin())
a.push(-20)
print(a.getMin(), a.getMin(), a.top(), a.getMin(), a.pop())
a.push()