-
Notifications
You must be signed in to change notification settings - Fork 0
/
043.py
76 lines (59 loc) · 1.96 KB
/
043.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Problem:
Implement a stack that has the following methods:
push(val), which pushes an element onto the stack
pop(), which pops off and returns the topmost element of the stack. If there are no
elements in the stack, then it should throw an error or return null.
max(), which returns the maximum value in the stack currently. If there are no elements
in the stack, then it should throw an error or return null.
Each method should run in constant time.
"""
from DataStructures.Stack import Stack
class MaxStack:
def __init__(self) -> None:
self.stack = Stack()
self.maximum = Stack()
def __repr__(self) -> str:
return f"Stack: {self.stack}\nMax Stack: {self.maximum}"
def push(self, val: int) -> None:
self.stack.push(val)
# if the current value is larger than the previous maxima, its index is added
# to the maximum stack
if self.maximum.is_empty() or self.stack[self.maximum.peek()] < val:
self.maximum.push(len(self.stack) - 1)
def pop(self) -> int:
if self.stack.is_empty():
raise RuntimeError("Cannot pop from a empty stack")
# if the index of the current element to be removed is in the maximum stack,
# its removed as well
if len(self.stack) == self.maximum.peek() + 1:
self.maximum.pop()
return self.stack.pop()
def max(self) -> int:
if self.stack.is_empty():
raise RuntimeError("Cannot get max of a empty stack")
# the maximum is accessed from the last poistion stored in maximum stack
return self.stack[self.maximum.peek()]
if __name__ == "__main__":
s = MaxStack()
s.push(1)
s.push(3)
s.push(2)
s.push(5)
print(s.max())
print(s)
print(s.pop())
print()
print(s.max())
print(s)
print(s.pop())
print()
print(s.max())
print(s)
print(s.pop())
print()
print(s.max())
print(s)
print(s.pop())
print()
print(s)