-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek1_stack.py
More file actions
31 lines (26 loc) · 929 Bytes
/
Copy pathweek1_stack.py
File metadata and controls
31 lines (26 loc) · 929 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
def stack_operation():
stack = []
max_stack = []
queries = int(input())
for i in range(queries):
query = input().strip()
query_parts = query.split()
if query_parts[0] == 'push':
stack.append(int(query_parts[1]))
if len(max_stack) == 0:
max_stack.append(int(query_parts[1]))
elif int(query_parts[1]) >= max_stack[-1]:
max_stack.append(int(query_parts[1]))
elif query_parts[0] == 'max':
print(max_stack[-1])
else:
last_elestack = stack[-1]
last_elemaxst = max_stack[-1]
if len(stack) != 0:
if last_elestack == last_elemaxst:
stack.pop()
max_stack.pop()
else:
stack.pop()
if __name__ == '__main__':
stack_operation()