LeetCode 题解工作台

课程表 IV

你总共需要上 numCourses 门课,课程编号依次为 0 到 numCourses-1 。你会得到一个数组 prerequisite ,其中 prerequisites[i] = [a i , b i ] 表示如果你想选 b i 课程,你 必须 先选 a i 课程。 有的课会有直接的先修课程,比…

category

4

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

中等 · 图·搜索

bolt

答案摘要

我们创建一个二维数组 ,其中 表示节点 到节点 是否可达。 接下来,我们遍历先修课程数组 ,对于其中的每一项 $[a, b]$,我们将 设为 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 图·搜索 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

你总共需要上 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 <= 100
  • 0 <= prerequisites.length <= (numCourses * (numCourses - 1) / 2)
  • prerequisites[i].length == 2
  • 0 <= ai, bi <= numCourses - 1
  • ai != bi
  • 每一对 [ai, bi] 都 不同
  • 先修课程图中没有环。
  • 1 <= queries.length <= 104
  • 0 <= ui, vi <= numCourses - 1
  • ui != vi
lightbulb

解题思路

方法一:Floyd 算法

我们创建一个二维数组 ff,其中 f[i][j]f[i][j] 表示节点 ii 到节点 jj 是否可达。

接下来,我们遍历先修课程数组 prerequisitesprerequisites,对于其中的每一项 [a,b][a, b],我们将 f[a][b]f[a][b] 设为 truetrue

然后,我们使用 Floyd 算法计算出所有节点对之间的可达性。

具体地,我们使用三重循环,首先枚举中间点 kk,接下来枚举起点 ii,最后枚举终点 jj。对于每一次循环,如果节点 ii 到节点 kk 可达,且节点 kk 到节点 jj 可达,那么节点 ii 到节点 jj 也是可达的,我们将 f[i][j]f[i][j] 设为 truetrue

在计算完所有节点对之间的可达性之后,对于每一个查询 [a,b][a, b],我们直接返回 f[a][b]f[a][b] 即可。

时间复杂度 O(n3)O(n^3),空间复杂度 O(n2)O(n^2)。其中 nn 为节点数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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]
speed

复杂度分析

指标
时间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)
psychology

面试官常问的追问

外企场景
  • 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?

warning

常见陷阱

外企场景
  • 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.

swap_horiz

进阶变体

外企场景
  • 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.

help

常见问题

外企场景

课程表 IV题解:图·搜索 | LeetCode #1462 中等