Max stack
Design a max-stack supporting push(x), pop() (remove and return top), top() (peek top), peek_max() (return the maximum), and pop_max() (remove and return the maximum; if there are ties, remove the one closest to the top). Replay operations ["push", x], ["pop"], ["top"], ["peekMax"], ["popMax"] and return the list of values returned by pop/top/peekMax/popMax in order. Assume queries are never made on an empty stack.
Implement
max_stack(ops: list[list]) → list[int]Examples
in
[[["push",5],["push",1],["push",5],["top"],["popMax"],["top"],["peekMax"],["pop"],["top"]]]out[5,5,1,5,1,5]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 35 min
solution.py
InputExpectedGot
[[["push",5],["push",1],["push",5],["top"],["popMax"],["top"],["peekMax"],["pop"],["top"]]][5,5,1,5,1,5]not run yetsample