I have implemented a PriorityQueue (Max heap) using Java API. Sometimes I need to remove an certain element from the queue, but the method remove()
takes O(n) time, is there any way I can improve that to for example O(logn) using this API? Or do I need to implement my own PQ by scratch?
I've tried using a HashMap to store the index of each element, but I have no idea of how to remove an element from the PQ based by the index, is this possible?
I guess in that case the idea would be to:
- Find the element by index
- Shift it up in O(logn) time
- Extract the element in constant time
Any ideas?