Skip to content

Network Delay Time

MediumGraphs
Decode it first. Clues, constraints, pattern and plan before any code.

A network has n servers labelled 1 to n. Each entry [u, v, w] in times is a one-way link: a message sent from u reaches v after w milliseconds. You send a ping from server k. Return how long it takes until every server has received it, or -1 if some server never does.

times = [[2, 1, 1], [2, 3, 1], [3, 4, 1]], n = 4, k = 2  →  2
times = [[1, 2, 1]], n = 2, k = 2                       →  -1   (links only go 1 → 2)

Constraints

  • 1 <= n <= 100
  • 1 <= k <= n
  • 0 <= times.length <= 6000
  • 0 <= w <= 100
  • The same (u, v) pair may appear more than once; the faster link wins.