Max Min Stack
We've already seen how to implement a stack using linked lists or dynamic arrays. Your challenge is going to be to build on this base implementation to create a stack with additional superpowers.
Create a MaxMinStack class that will return the maximum and minimum value from the stack at any time using getStackMax and getStackMin functions.
You will start with the MaxMinStack class below, which already works as a basic stack. You can add or update any method necessary for this to work. The stack will only be passed integers, and pop, peek, getStackMax and getStackMin won't be called on an empty stack.
Breakdown
Validate My Answer
A brute force solution would be to iterate the stack to track the max/min integer. This would require O(n) time, so we should be looking for solutions better than this.
The max and min can increase or decrease at any point depending on what integer you
push/pop. Make sure you account for this.You can solve this in O(1) time and O(n) space.
Max Min Stack
Create a MaxMinStack class that will return the maximum and minimum value from the stack at any time using getStackMax and getStackMin functions.
You will start with the MaxMinStack class below, which already works as a basic stack. You can add or update any method necessary for this to work. The stack will only be passed integers, and pop, peek, getStackMax and getStackMin won't be called on an empty stack.