We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
No problem! I didn't use pq.remove() for that reason. Instead I performed an additional check for whether a node was already visited after removing it from the queue.
Doing it this way means that your queue can get full of lots of low-priority nodes that you've already visited (which I think slows down adding nodes). That said, whichever instance of a node dequeues first is guaranteed to be the highest priority one, and dequeue is only O(1).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Dijkstra: Shortest Reach 2
You are viewing a single comment's thread. Return to all comments →
No problem! I didn't use pq.remove() for that reason. Instead I performed an additional check for whether a node was already visited after removing it from the queue.
Doing it this way means that your queue can get full of lots of low-priority nodes that you've already visited (which I think slows down adding nodes). That said, whichever instance of a node dequeues first is guaranteed to be the highest priority one, and dequeue is only O(1).