716. Max Stack
Description
Intuition
We cannot get all operations to O(1).
Either push(x) or popMax() needs to be Log(N).
Remeber
- If we need
push(x)more efficient, we can have 2 stacks, one of which stores the value while the other stores the max value. - If we need
popMax()more effcient, we need create have a map which maps fromkeyto aLinkedList, and a normal double linked list.
Pitfall
Solution
Reference
Prove No O(1) Solution-isn't-possible)