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 fromkey
to aLinkedList
, and a normal double linked list.
Pitfall
Solution
Reference
Prove No O(1) Solution-isn't-possible)