Network Delay Time
1Try it yourself first
Think for 20-30 minutes before you open any hint. Being stuck is where the learning happens.
Only for you, saved in this browser. After you see the approach you will compare it with your idea.2What is being asked
A signal starts at node k. times[i] = [from, to, delay]. How long until all n nodes receive it? Return -1 if some node never does.
3Hint ladder
Take one rung at a time. Stop as soon as the idea clicks.
- Hint 1Why does a plain BFS give the wrong answer with different delays?
- Hint 2Always process the node with the smallest arrival time next.
4The approach
Have you tried the hints? The approach is built as a path from the obvious way to the clever one.
A rumour spreading through a town where each lane takes some minutes to walk. At any moment the next person to hear it is the one who can be reached soonest from the people who already know. Keep a to-do list sorted by arrival time (a min-heap): take the soonest arrival, if that person is new record their time and add the people they can reach with arrival = their time + the lane's minutes. The whole town knows it when the last person is told.
- The obvious way
Bellman-Ford relaxes every edge n times: O(n x edges), correct but slower.
- The key insight
Build lists of (neighbour, delay) per node. Heap holds (arrival time, node), starting with (0, k). Pop the smallest; if the node was already settled skip it; else record its arrival time and push each unsettled neighbour with time + delay. If all n nodes got a time, the answer is the largest of them; otherwise -1.
- The algorithm
- Build neighbours with delays; heap = [(0, k)].
- Pop the earliest arrival; skip if already settled.
- Record its time; push neighbours with time + delay.
- All nodes settled: return the largest time; else -1.
Watch it run
Exampletimes = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Signal starts at node 2 at time 0.
heard: 2 at 0
Where people slip
- Dijkstra needs non-negative delays; a negative one breaks it.
- Skip already settled nodes when popping; the heap can hold stale entries.
How close was your idea?
You did not write an idea, and that is fine.
Be honest with yourself. This sets your revision list.
5Code
Try writing it yourself from the steps first. Then compare.
import heapq
def network_delay_time(times, n, k):
lanes = {node: [] for node in range(1, n + 1)}
for start, end, minutes in times:
lanes[start].append((end, minutes))
arrivals = [(0, k)] # min-heap of (arrival time, person)
heard_at = {}
while arrivals:
time, person = heapq.heappop(arrivals)
if person in heard_at:
continue # already told earlier
heard_at[person] = time
for friend, minutes in lanes[person]:
if friend not in heard_at:
heapq.heappush(arrivals, (time + minutes, friend))
return max(heard_at.values()) if len(heard_at) == n else -1Now close this and write it again from memory, in the language you will use in the interview. Reattempt after 1 day, 7 days and about 30 days.
6Still stuck? Watch a video
Seeing someone explain it out loud can make it click. Try the video after you have read the approach above.