Skip to content

Commit fd74992

Browse files
committed
Added #43
1 parent ed4467b commit fd74992

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"""
2+
#43
3+
Amazon
4+
5+
Implement a stack that has the following methods:
6+
-> push(val), which pushes an element onto the stack
7+
8+
-> pop(), which pops off and returns the topmost element of the stack.
9+
If there are no elements in the stack, then it should throw an error or return null.
10+
11+
-> max(), which returns the maximum value in the stack currently.
12+
If there are no elements in the stack, then it should throw an error or return null.
13+
Each method should run in constant time.
14+
"""
15+
16+
# Node represents a single stack element
17+
class Node:
18+
def __init__(self, val):
19+
self.val = val
20+
self.prev = None
21+
22+
def __str__(self):
23+
return "<" + str(self.val) + ">"
24+
25+
# Ordinary stack implementation with linked-list
26+
class Stack:
27+
def __init__(self):
28+
self.top = Node(None)
29+
30+
def __str__(self):
31+
stringRepresentation = "< "
32+
currentNode = self.top
33+
while currentNode.val != None:
34+
stringRepresentation += str(currentNode)
35+
currentNode = currentNode.prev
36+
stringRepresentation += " >"
37+
return stringRepresentation
38+
39+
def push(self, val):
40+
newNode = Node(val)
41+
newNode.prev = self.top
42+
self.top = newNode
43+
44+
def pop(self):
45+
temp = self.top
46+
if temp.val != None:
47+
self.top = self.top.prev
48+
return temp
49+
50+
def peek(self):
51+
return self.top.val
52+
53+
# Special Stack inherits Stack and has additional features like max()
54+
class SpecialStack(Stack):
55+
def __init__(self):
56+
Stack.__init__(self)
57+
self.maxStack = Stack() # maxStack is an ordinary stack that is used to hold the max values.
58+
59+
"""
60+
push(x)
61+
1) Push x into Special Stack
62+
2) Compare x with the maxStack's top (y)
63+
-> If x > y, push x onto maxStack
64+
-> If y > x, push y onto maxStack
65+
"""
66+
def push(self, val):
67+
Stack.push(self,val)
68+
maxTop = self.getMaxTop()
69+
if maxTop == None or maxTop <= val:
70+
self.maxStack.push(val)
71+
else:
72+
self.maxStack.push(maxTop)
73+
74+
"""
75+
pop()
76+
1) Pop an element from maxStack
77+
2) Pop and return an element from Special Stack
78+
"""
79+
def pop(self):
80+
self.maxStack.pop()
81+
return Stack.pop(self)
82+
83+
def getMaxTop(self):
84+
return self.maxStack.peek()
85+
86+
def max(self):
87+
return self.getMaxTop()
88+
89+
90+
def main():
91+
s = SpecialStack()
92+
93+
s.push(18)
94+
print(s, s.maxStack, s.max())
95+
s.push(19)
96+
print(s, s.maxStack, s.max())
97+
s.push(29)
98+
print(s, s.maxStack, s.max())
99+
s.push(15)
100+
print(s, s.maxStack, s.max())
101+
s.push(16)
102+
print(s, s.maxStack, s.max())
103+
104+
s.pop()
105+
print(s, s.maxStack, s.max())
106+
s.pop()
107+
print(s, s.maxStack, s.max())
108+
s.pop()
109+
print(s, s.maxStack, s.max())
110+
s.pop()
111+
print(s, s.maxStack, s.max())
112+
s.pop()
113+
print(s, s.maxStack, s.max())
114+
115+
if __name__ == "__main__":
116+
main()

index.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
* [#29 Run Length Encoding](./challenges/run-length-encoding.py)
3131
* [#36 Second Largest Node In BST](./challenges/second-largest-node-in-bst.py)
3232
* [#42 SubArray Sum](./challenges/subarray-sum.py)
33+
* [#43 Max Stack Implementation](./challenges/max-stack-implementation.py)
3334
* [#55 URL Shortener](./challenges/url-shortener.py)
3435
* [#157 Is Permutation Palindrome](./challenges/is-permutation-palindrome.py)
3536
* [#171 Busiest Time In Building](./challenges/busiest-time-in-building.py)
3637
* [#173 Flatten Nested Dictionary](./challenges/flatten-nested-dictionary.py)
3738

3839

39-
40-
4140
## Lessons
4241

4342
* [Merge Sort](./lessons/merge-sort.py)

0 commit comments

Comments
 (0)