TRT
Sign in
mediumAdvancedLeetCode#Dijkstra

Network delay time

Find how long a signal takes to reach every node of a weighted directed network, with Dijkstra from the source.

The Runtime Theory Team1 min read
Taught inDijkstra's algorithmPro — members onlyWith non-negative edge weights, the unsettled vertex with the smallest tentative distance can never be improved, so Dijkstra's algorithm settles vertices one at a time in distance order, using a min-heap.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

intimes = [[2, 1, 1], [2, 3, 1], [3, 4, 1]], n = 4, k = 2

out2

intimes = [[1, 2, 1]], n = 2, k = 2

out-1

intimes = [[1, 2, 4], [1, 3, 1], [3, 2, 1]], n = 3, k = 1

out2

times[i] = (u, v, w) means a signal takes w to travel from u to v. Send a signal from node k. Return the time until all n nodes have received it, or -1 if some never do.

Before you code

Each node receives the signal at its shortest-path distance from k. The answer is the largest of those distances.

Hints

  1. Build adjacency lists and run Dijkstra from k with a min-heap.
  2. Skip stale heap entries whose distance is larger than the recorded one.
  3. If fewer than n nodes are reached, return -1; otherwise the maximum distance.

Production & complexity notes

When solving Network delay time on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Dijkstra's algorithm to reinforce the underlying pattern.

What it practices

Dijkstra end to end, from Dijkstra's algorithm.

Not started

Sign in to save your learning progress.

Sign in to save