From this, we can design data structure special stack with method getMin()
which should return minimum element from the SpecialStack.
My question is: How to implement the method getMed()
which should return the medium element from the SpecailStack?
From this Data Structure to find median, we know the best data structure is two heaps. Left
is a Max-Heap
; Right
is a Min-Heap
. However, for my question it seems not good, because the top element pushed into stack must be maintained which heap can not do that. Am I right?
Edit I do not know how to maintain the index of latest pushed element with Heap.
Thanks a lot.