Network Delay Time
LeetCode 743 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Dijkstra from k; answer = max distance.
நான் Network Delay Time (LeetCode 743) ஐ இப்படித் தீர்த்தேன் — Shortest Path / Dijkstra பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
The signal reaches each node along its shortest path from k, so the time for everyone to receive it is the largest shortest-path distance. Dijkstra computes all of them.
Approach
- 1
Adjacency list of weighted directed edges.
- 2
Min-heap starting with (0, k).
- 3
Relax edges; push improved distances.
- 4
Max of dist[1..n], or −1 if any is ∞.
நான் எப்படித் தீர்த்தேன்
DijkstraPythonO(E log V) time
class Solution:
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
adj = defaultdict(list)
for u,v,w in times:
adj[u].append((v,w))
dist = [float("inf")]*(n+1)
dist[k]=0
min_heap =[(0,k)]
while min_heap:
cost,node = heapq.heappop(min_heap)
for adj_node,wt in adj[node]:
if dist[node]+wt < dist[adj_node]:
dist[adj_node]=dist[node]+wt
heapq.heappush(min_heap,(dist[node]+wt,adj_node))
min_time = max(dist[1:])
return min_time if min_time!=float("inf") else -1Network Delay Time — அடிக்கடி கேட்கப்படும் கேள்விகள்
Network Delay Time பின்னாலுள்ள intuition என்ன?
The signal reaches each node along its shortest path from k, so the time for everyone to receive it is the largest shortest-path distance. Dijkstra computes all of them.
Network Delay Time (LeetCode 743) ஐ எப்படித் தீர்ப்பது?
1. Adjacency list of weighted directed edges. 2. Min-heap starting with (0, k). 3. Relax edges; push improved distances. 4. Max of dist[1..n], or −1 if any is ∞.
Network Delay Time இன் time மற்றும் space complexity என்ன?
Time: O(E log V). Space: O(V + E). (Accepted approach: Dijkstra.)
Network Delay Time இல் பொதுவான தவறுகள் என்ன?
Nodes are 1-indexed; ignore dist[0].