if self empty ()raise indexerror ('pop ()empty stack 'pop_element self _element pop (current_max self _cached_max_with_count - max if pop_element =current_max self _cached_max_with_count - count - if self _cached_max_with_count - count = self _cached_max_with_count pop (return pop_element def push(self )self _element append (xif len(self _cached_max_with_count = self _cached_max_with_count append (self maxwithcount ( )elsecurrent_max self _cached_max_with_count - max if =current_max self _cached_max_with_count - count + elif current_max self _cached_max_with_count append (self maxwithcount ( )the worst-case additional space complexity is ( )which occurs when each key pushed is greater than all keys in the primary stack howeverwhen the number of distinct keys is smallor the maximum changes infrequentlythe additional space complexity is lesso( in the best-case the time complexity for each specified method is still ( aux , aux , , , , , , , , , aux , aux aux aux aux , , , , , , , , , , , aux aux aux aux , aux , aux , , aux figure the primary and auxiliary stacks for the following operationspush push push push push push push poppoppoppoppush push both stacks are initially emptyand their progression is shown from left-to-rightthen top-to-bottom the top of the auxiliary stack holds the maximum element in the stackand the number of times that element occurs in the stack the auxiliary stack is denoted by aux