27 · Shortest Paths

#743 Network Delay Time

Mediumsolve on LeetCode ↗

frequently reported atNetflixGoogleAmazon

the problem

In plain words

A signal starts at node k in a directed, weighted network of n nodes (labelled 1..n). times[i] = [u, v, w] means it takes w time to travel from u to v. Return how long until every node has the signal, or -1 if some node never gets it.

in times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2

out 2

Node 4 is the last to hear it: 2→3→4 takes 2.

in times = [[1,2,1]], n = 2, k = 1

out 1

in times = [[1,2,1]], n = 2, k = 2

out -1

From node 2 the signal can't reach node 1.

  • 1 ≤ k ≤ n ≤ 100
  • 1 ≤ times.length ≤ 6000
  • 0 ≤ w ≤ 100
  • All (u, v) pairs are unique

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #1514
      Path with Maximum Probability
      Mediumin Shortest Paths
    2. #787
      Cheapest Flights Within K Stops
      Mediumin Shortest Paths
    3. #994
      Rotting Oranges
      Mediumin Graphs