Graphs · Problem 3 of 3

How long until news reaches everyone?

Pattern: Network Delay Time with Dijkstra

hardNot solved yet

The problem

A company has offices numbered 0 to offices - 1. Each link [a, b, minutes] means office a can pass news to office b, taking minutes (always 0 or more). Links only work in the stated direction. News starts at office start at minute 0, and every office forwards it as soon as it arrives.

Write a function that returns how many minutes it takes until every office has the news, or -1 if some office never gets it.

text
1time_to_reach_all(3, [[0, 1, 4], [0, 2, 1], [2, 1, 2]], 0)  ->  3
2time_to_reach_all(2, [[1, 0, 5]], 0)                        ->  -1
3time_to_reach_all(1, [], 0)                                 ->  0
Example 1
41200 min13 min21 min

Going through office 2 (1 + 2 = 3 minutes) beats the direct 4-minute link.

In the first example, news reaches office 2 at minute 1, then office 1 at minute 3 through office 2, which beats the direct 4-minute link.

Write time_to_reach_all(offices, links, start) in Python or timeToReachAll(offices, links, start) in JavaScript.

Runs privately in your browser. Nothing is sent to a server.Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Write your function, then run it against 22 tests.

Breakdown

Stuck? Reveal one step at a time, and try again after each one.

  1. Step 2
  2. Step 3
  3. Step 4

Solution

?

Give it a real try first: you learn far more from a wrong attempt than from reading the answer.