bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 743. Network Delay Time

743. Network Delay Time

Leetcode Solutions

Approach: Dijkstra's Algorithm

  1. Create an adjacency list from the times array, where each entry adj[source] contains a list of pairs (time, dest) representing the travel time and destination node for each edge.
  2. Initialize an array signalReceivedAt with a large value for all nodes to represent infinity.
  3. Set signalReceivedAt[k] to 0 since the signal starts from node k.
  4. Add (0, k) to the priority queue, where 0 is the current shortest distance to k.
  5. While the priority queue is not empty: a. Pop the node currNode with the smallest distance currDist from the queue. b. If currDist is greater than signalReceivedAt[currNode], skip to the next iteration. c. For each (time, neighborNode) in adj[currNode], if currDist + time is less than signalReceivedAt[neighborNode], update signalReceivedAt[neighborNode] and add (currDist + time, neighborNode) to the queue.
  6. After processing all nodes, find the maximum value in signalReceivedAt. If it's still the initial large value for any node, return -1. Otherwise, return the maximum value.
UML Thumbnail

Approach: Depth-First Search (DFS)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR