#743

Network Delay Time

Medium Dijkstra

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

A signal starts at node k in a directed weighted graph. Return how long until every node receives it, or -1 if some never do.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: how long until a signal from k reaches every node?

Why this works: each node's arrival time is its shortest distance from k. Dijkstra with a min-heap finds them; the answer is the largest.

Example: edges 2→1 (1), 2→3 (1), 3→4 (1), k = 2

dist: 1→1, 3→1, 4→2 → answer 2

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Not skipping stale heap entries (d > dist[u]), which wastes time on outdated paths.

Solve on LeetCode ↗
Complexity
  • Time: O(E log V).
  • Space: O(V + E).
Topic: Graphs
🎬 Episodes 74–85: video coming soon