-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathf_stack_max.py
38 lines (31 loc) · 936 Bytes
/
f_stack_max.py
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
class StackMax:
def __init__(self):
self._stack = []
self._max = []
def push(self, value: int):
self._stack.append(value)
if not self._max or value >= self._max[-1]:
self._max.append(value)
def pop(self):
if not self._stack:
return 'error'
value = self._stack.pop()
if self._max[-1] == value:
self._max.pop()
return None
def get_max(self):
if not self._max:
return 'None'
return self._max[-1]
def execute(command: str, stack: StackMax) -> None:
method_name, *args = command.split()
args = list(map(int, args))
return_value = getattr(stack, method_name)(*args)
if return_value is not None:
print(return_value)
if __name__ == '__main__':
n = int(input())
stack = StackMax()
for _ in range(n):
command = input()
execute(command, stack)