Network Delay Time

Graphs, problem 7 of 8

Network Delay Time

Medium

LC #743

dijkstrashortest pathheap

Not attempted yet

A network has n nodes labelled 1..n. Each entry [u, v, w] in times is a directed link: a signal sent from u reaches v after w time units.

A signal starts at node k. Return the time it takes for all nodes to receive it, or -1 if some node never does.

Example 1

Input: times = [[2,1,1],[2,3,1],[3,4,1]],
       n = 4, k = 2
Output: 2

Node 4 is reached last: 2 → 3 → 4.

Example 2

Input: times = [[1,2,1]], n = 2, k = 1
Output: 1

Example 3

Input: times = [[1,2,1]], n = 2, k = 2
Output: -1

Links are one-way: node 1 is unreachable from 2.

Constraints

  • 1 <= n <= 10^4, 0 <= len(times) <= 3 * 10^4
  • 0 <= w <= 100
  • there may be several links between the same pair

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.