-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_with_max.py
42 lines (34 loc) · 992 Bytes
/
stack_with_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
39
40
41
42
import sys
class StackWithMax():
def __init__(self):
self.__stack = []
self.auxstack=[]
def Push(self, a):
self.__stack.append(a)
if(self.auxstack):
if self.auxstack[-1]<a:
self.auxstack.append(a)
else:
self.auxstack.append(self.auxstack[-1])
else:
self.auxstack.append(a)
def Pop(self):
assert(len(self.__stack))
self.__stack.pop()
self.auxstack.pop()
def Max(self):
assert(len(self.__stack))
return (self.auxstack[-1])
if __name__ == '__main__':
stack = StackWithMax()
num_queries = int(sys.stdin.readline())
for _ in range(num_queries):
query = sys.stdin.readline().split()
if query[0] == "push":
stack.Push(int(query[1]))
elif query[0] == "pop":
stack.Pop()
elif query[0] == "max":
print(stack.Max())
else:
assert(0)