Skip to content
Onsite

Network Delay Time

MediumP2Coding

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

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

Example 1

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

Example 2

Input
times = [[1,2,1]], n = 2, k = 2
Output
-1
Why
Node 2 has no outgoing edge, so node 1 is never reached.

Constraints

  • 1 <= k <= n <= 100
  • 1 <= times.length <= 6000
  • 1 <= u, v <= n, u != v
  • 0 <= w <= 100

Hints

Run checks the 3 visible tests, Submit all 9.