forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.py
34 lines (33 loc) · 921 Bytes
/
sol.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
class Stack(list):
"""
A lot of cheating, use python's in-built lists a lot
"""
def __init__(self):
super().__init__()
self.maxElement = -10**10
self.max_ending_here = list()
def push(self, val):
self.append(val)
self.maxElement = max(val, self.maxElement)
self.max_ending_here.append(self.maxElement)
def popStack(self):
if self.__len__()==0:
raise Exception("Empty Stack")
self.pop()
self.max_ending_here.pop()
def max(self):
if self.__len__()>0:
return self.max_ending_here[-1]
else:
raise Exception("Empty Stack")
if __name__=='__main__':
import random
s = Stack()
for _ in range(10):
s.push(random.randint(-100, 100))
print(s)
print(s.max())
for _ in range(10):
s.popStack()
print(s)
print(s.max())