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
- Build adjacency lists and run Dijkstra from k with a min-heap.
- Skip stale heap entries whose distance is larger than the recorded one.
- 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.