Back to problems

MaxStack

Object-Oriented Programming · ByteDance · Medium

Design MaxStack, a data structure that behaves like an ordinary stack of integers but additionally keeps track of the largest value it currently contains. It must support the following operations: push(x): place the integer x on top of the stack. pop(): remove and return the element currently on top of the stack. top(): return the element on top without removing it. peekMax(): return the largest value currently held in the stack, leaving the contents unchanged. popMax():…

Checking your access…