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 ()
0 commit comments