LeetCode 题解工作台
颜色交替的最短路径
给定一个整数 n ,即有向图中的节点数,其中节点标记为 0 到 n - 1 。图中的每条边为红色或者蓝色,并且可能存在自环或平行边。 给定两个数组 redEdges 和 blueEdges ,其中: redEdges[i] = [a i , b i ] 表示图中存在一条从节点 a i 到节点 b i…
2
题型
5
代码语言
3
相关题
当前训练重点
中等 · 图·搜索
答案摘要
题目实际上是最短路问题,我们可以考虑使用 BFS 来解决。 首先,我们对所有的边进行预处理,将所有的边按照颜色分类,存储到多维数组 中。其中 存储所有红色边,而 存储所有蓝色边。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 图·搜索 题型思路
题目描述
给定一个整数 n,即有向图中的节点数,其中节点标记为 0 到 n - 1。图中的每条边为红色或者蓝色,并且可能存在自环或平行边。
给定两个数组 redEdges 和 blueEdges,其中:
redEdges[i] = [ai, bi]表示图中存在一条从节点ai到节点bi的红色有向边,blueEdges[j] = [uj, vj]表示图中存在一条从节点uj到节点vj的蓝色有向边。
返回长度为 n 的数组 answer,其中 answer[X] 是从节点 0 到节点 X 的红色边和蓝色边交替出现的最短路径的长度。如果不存在这样的路径,那么 answer[x] = -1。
示例 1:
输入:n = 3, red_edges = [[0,1],[1,2]], blue_edges = [] 输出:[0,1,-1]
示例 2:
输入:n = 3, red_edges = [[0,1]], blue_edges = [[2,1]] 输出:[0,1,-1]
提示:
1 <= n <= 1000 <= redEdges.length, blueEdges.length <= 400redEdges[i].length == blueEdges[j].length == 20 <= ai, bi, uj, vj < n
解题思路
方法一:BFS
题目实际上是最短路问题,我们可以考虑使用 BFS 来解决。
首先,我们对所有的边进行预处理,将所有的边按照颜色分类,存储到多维数组 中。其中 存储所有红色边,而 存储所有蓝色边。
接着,我们定义以下数据结构或变量:
- 队列 :用来存储当前搜索到的节点,以及当前边的颜色;
- 集合 :用来存储已经搜索过的节点,以及当前边的颜色;
- 变量 :用来表示当前搜索的层数,即当前搜索到的节点到起点的距离;
- 数组 :用来存储每个节点到起点的最短距离。初始时,我们将 数组中的所有元素初始化为 ,表示所有节点到起点的距离都未知。
我们首先将起点 和起点边的颜色 或 入队,表示从起点出发,且当前是红色或蓝色边。
接下来,我们开始进行 BFS 搜索。我们每次从队列中取出一个节点 ,如果当前节点的答案还未更新,则将当前节点的答案更新为当前层数 ,即 。然后,我们将当前边的颜色 取反,即如果当前边为红色,则将其变为蓝色,反之亦然。我们取出颜色对应的所有边,如果边的另一端节点 未被搜索过,则将其入队。
搜索结束后,返回答案数组即可。
时间复杂度 ,空间复杂度 。其中 和 分别为节点数和边数。
class Solution:
def shortestAlternatingPaths(
self, n: int, redEdges: List[List[int]], blueEdges: List[List[int]]
) -> List[int]:
g = [defaultdict(list), defaultdict(list)]
for i, j in redEdges:
g[0][i].append(j)
for i, j in blueEdges:
g[1][i].append(j)
ans = [-1] * n
vis = set()
q = deque([(0, 0), (0, 1)])
d = 0
while q:
for _ in range(len(q)):
i, c = q.popleft()
if ans[i] == -1:
ans[i] = d
vis.add((i, c))
c ^= 1
for j in g[c][i]:
if (j, c) not in vis:
q.append((j, c))
d += 1
return ans
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | Depends on the final approach |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidates should demonstrate understanding of BFS and how it applies to graph traversal with color alternation.
- question_mark
Look for clarity in representing the problem as a graph and alternating edge colors during BFS.
- question_mark
Candidates should be able to efficiently manage the BFS state transitions between red and blue edges.
常见陷阱
外企场景- error
Not alternating between red and blue edges properly, causing incorrect paths or infinite loops.
- error
Not handling the case where no valid path exists, leading to incorrect answers.
- error
Mismanaging the state transitions between nodes and edge colors during BFS, causing missed nodes or incorrect distances.
进阶变体
外企场景- arrow_right_alt
Change the problem to find paths with a specific number of alternating edges.
- arrow_right_alt
Modify the graph to include weighted edges and ask for the shortest path with alternating edge colors.
- arrow_right_alt
Allow multiple starting nodes and find the shortest alternating path from each starting point.