Ctrl K

Broadcast Reach Time

Medium
A network has node_count nodes numbered 1 to node_count. Links are one-way: each entry links[i] = [from, to, delay] means a signal sent from node from arrives at node to after delay time units. There is at most one link for each ordered pair of nodes. A signal is sent from node source, and every node that receives it immediately forwards it along all of its outgoing links. Return how long it takes until every node in the network has the signal. If some node never gets it, return -1.
Example 1
links=[[1, 2, 4], [1, 3, 1], [3, 2, 2], [2, 4, 1]], node_count=4, source=1 → 4

The signal reaches node 3 after 1, node 2 faster via node 3 after 1 + 2 = 3, and node 4 one unit later, at 4. Node 4 is the last to hear it.

Example 2
links=[[2, 1, 3]], node_count=3, source=2 → -1

No link leads into node 3, so not every node can be reached.

Constraints
  • 1 ≤ source ≤ node_count ≤ 100
  • 1 ≤ len(links) ≤ 6000
  • 1 ≤ from, to ≤ node_count, from ≠ to
  • 0 ≤ delay ≤ 100
  • The (from, to) pairs are unique
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Path of a Rolling Ball MediumCheapest Trip With Few Layovers MediumBall Into the Hole HardRoads on the Fastest Routes HardRatio Chain Queries Medium
loading Python…
Input
links=[[1, 2, 4], [1, 3, 1], [3, 2, 2], [2, 4, 1]], node_count=4, source=1
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.