LeetCode 题解工作台
网络延迟时间
有 n 个网络节点,标记为 1 到 n 。 给你一个列表 times ,表示信号经过 有向 边的传递时间。 times[i] = (u i , v i , w i ) ,其中 u i 是源节点, v i 是目标节点, w i 是一个信号从源节点传递到目标节点的时间。 现在,从某个节点 K 发出一个信…
5
题型
5
代码语言
3
相关题
当前训练重点
中等 · 图·DFS·traversal
答案摘要
我们定义 表示节点 到节点 的边权,如果节点 到节点 之间没有边,则 $\textit{g}[u][v] = +\infty$。 我们维护一个数组 ,其中 表示节点 到节点 的最短路径长度。初始时,我们将 全部初始化为 ,但 $\textit{dist}[k - 1] = 0$。定义一个数组 ,其中 表示节点 是否被访问过,初始时,我们将 全部初始化为 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 图·DFS·traversal 题型思路
题目描述
有 n 个网络节点,标记为 1 到 n。
给你一个列表 times,表示信号经过 有向 边的传递时间。 times[i] = (ui, vi, wi),其中 ui 是源节点,vi 是目标节点, wi 是一个信号从源节点传递到目标节点的时间。
现在,从某个节点 K 发出一个信号。需要多久才能使所有节点都收到信号?如果不能使所有节点收到信号,返回 -1 。
示例 1:

输入:times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2 输出:2
示例 2:
输入:times = [[1,2,1]], n = 2, k = 1 输出:1
示例 3:
输入:times = [[1,2,1]], n = 2, k = 2 输出:-1
提示:
1 <= k <= n <= 1001 <= times.length <= 6000times[i].length == 31 <= ui, vi <= nui != vi0 <= wi <= 100- 所有
(ui, vi)对都 互不相同(即,不含重复边)
解题思路
方法一:朴素 Dijkstra 算法
我们定义 表示节点 到节点 的边权,如果节点 到节点 之间没有边,则 。
我们维护一个数组 ,其中 表示节点 到节点 的最短路径长度。初始时,我们将 全部初始化为 ,但 。定义一个数组 ,其中 表示节点 是否被访问过,初始时,我们将 全部初始化为 。
我们每次找到未被访问的距离最小的节点 ,然后以节点 为中心进行松弛操作,即对于每个节点 ,如果 ,则更新 。
最后,我们返回 中的最大值,即为答案。如果答案为 ,则说明存在无法到达的节点,返回 。
时间复杂度 ,空间复杂度 。其中 和 分别为节点数和边数。
class Solution:
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
g = [[inf] * n for _ in range(n)]
for u, v, w in times:
g[u - 1][v - 1] = w
dist = [inf] * n
dist[k - 1] = 0
vis = [False] * n
for _ in range(n):
t = -1
for j in range(n):
if not vis[j] and (t == -1 or dist[t] > dist[j]):
t = j
vis[t] = True
for j in range(n):
dist[j] = min(dist[j], dist[t] + g[t][j])
ans = max(dist)
return -1 if ans == inf else ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
The candidate is expected to demonstrate knowledge of graph traversal methods and the ability to optimize with a priority queue or BFS.
- question_mark
Be prepared for candidates to compare DFS and BFS as options, with an emphasis on using the most efficient method for large input sizes.
- question_mark
A successful answer should include handling edge cases, such as unreachable nodes or cycles, and the reasoning for selecting the approach used.
常见陷阱
外企场景- error
Failing to correctly handle the case where not all nodes can receive the signal, leading to an incorrect return value of -1.
- error
Incorrectly assuming all graphs are uniform, which may lead to inefficient solutions or improper use of BFS/Dijkstra.
- error
Not accounting for the possibility of cycles in the graph, which could lead to an infinite loop or incorrect time calculations.
进阶变体
外企场景- arrow_right_alt
A variation of this problem could involve multiple source nodes for the signal, testing the candidate's ability to modify their approach to handle additional starting points.
- arrow_right_alt
Another variant could involve a directed graph with negative edge weights, which would require considering algorithms like Bellman-Ford instead of Dijkstra or BFS.
- arrow_right_alt
The problem could also be modified by adding additional constraints, such as limiting the number of allowed edges, which could test the candidate's ability to optimize graph traversal techniques.