LeetCode 题解工作台
课程表 IV
你总共需要上 numCourses 门课,课程编号依次为 0 到 numCourses-1 。你会得到一个数组 prerequisite ,其中 prerequisites[i] = [a i , b i ] 表示如果你想选 b i 课程,你 必须 先选 a i 课程。 有的课会有直接的先修课程,比…
4
题型
5
代码语言
3
相关题
当前训练重点
中等 · 图·搜索
答案摘要
我们创建一个二维数组 ,其中 表示节点 到节点 是否可达。 接下来,我们遍历先修课程数组 ,对于其中的每一项 $[a, b]$,我们将 设为 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 图·搜索 题型思路
题目描述
你总共需要上 numCourses 门课,课程编号依次为 0 到 numCourses-1 。你会得到一个数组 prerequisite ,其中 prerequisites[i] = [ai, bi] 表示如果你想选 bi 课程,你 必须 先选 ai 课程。
- 有的课会有直接的先修课程,比如如果想上课程
1,你必须先上课程0,那么会以[0,1]数对的形式给出先修课程数对。
先决条件也可以是 间接 的。如果课程 a 是课程 b 的先决条件,课程 b 是课程 c 的先决条件,那么课程 a 就是课程 c 的先决条件。
你也得到一个数组 queries ,其中 queries[j] = [uj, vj]。对于第 j 个查询,您应该回答课程 uj 是否是课程 vj 的先决条件。
返回一个布尔数组 answer ,其中 answer[j] 是第 j 个查询的答案。
示例 1:

输入:numCourses = 2, prerequisites = [[1,0]], queries = [[0,1],[1,0]] 输出:[false,true] 解释:[1, 0] 数对表示在你上课程 0 之前必须先上课程 1。 课程 0 不是课程 1 的先修课程,但课程 1 是课程 0 的先修课程。
示例 2:
输入:numCourses = 2, prerequisites = [], queries = [[1,0],[0,1]] 输出:[false,false] 解释:没有先修课程对,所以每门课程之间是独立的。
示例 3:

输入:numCourses = 3, prerequisites = [[1,2],[1,0],[2,0]], queries = [[1,0],[1,2]] 输出:[true,true]
提示:
2 <= numCourses <= 1000 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2)prerequisites[i].length == 20 <= ai, bi <= numCourses - 1ai != bi- 每一对
[ai, bi]都 不同 - 先修课程图中没有环。
1 <= queries.length <= 1040 <= ui, vi <= numCourses - 1ui != vi
解题思路
方法一:Floyd 算法
我们创建一个二维数组 ,其中 表示节点 到节点 是否可达。
接下来,我们遍历先修课程数组 ,对于其中的每一项 ,我们将 设为 。
然后,我们使用 Floyd 算法计算出所有节点对之间的可达性。
具体地,我们使用三重循环,首先枚举中间点 ,接下来枚举起点 ,最后枚举终点 。对于每一次循环,如果节点 到节点 可达,且节点 到节点 可达,那么节点 到节点 也是可达的,我们将 设为 。
在计算完所有节点对之间的可达性之后,对于每一个查询 ,我们直接返回 即可。
时间复杂度 ,空间复杂度 。其中 为节点数。
class Solution:
def checkIfPrerequisite(
self, n: int, prerequisites: List[List[int]], queries: List[List[int]]
) -> List[bool]:
f = [[False] * n for _ in range(n)]
for a, b in prerequisites:
f[a][b] = True
for k in range(n):
for i in range(n):
for j in range(n):
if f[i][k] and f[k][j]:
f[i][j] = True
return [f[a][b] for a, b in queries]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(N^3 + Q) because building the reachability matrix involves O(N^3) operations for N courses, and answering Q queries is O(Q). Space complexity is O(N^2) to store the reachability matrix. |
| 空间 | O(N^2) |
面试官常问的追问
外企场景- question_mark
Are you considering both direct and indirect prerequisites?
- question_mark
Can you optimize repeated queries without running DFS each time?
- question_mark
How does topological ordering simplify the prerequisite propagation?
常见陷阱
外企场景- error
Ignoring indirect prerequisites leads to incorrect query answers.
- error
Attempting DFS/BFS per query may exceed time limits on large inputs.
- error
Failing to initialize or update the reachability matrix correctly during topological processing.
进阶变体
外企场景- arrow_right_alt
Check if all courses can be completed given the same prerequisites graph.
- arrow_right_alt
Determine the minimum number of courses required to satisfy a set of query constraints.
- arrow_right_alt
Compute the longest chain of prerequisites instead of answering individual queries.