Nav: << previous: 206.反转链表 | next: 208.实现 Trie (前缀树) >>
Description
tab: English
<p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i</sub></code> first if you want to take course <code>a<sub>i</sub></code>.</p>
<ul>
<li>For example, the pair <code>[0, 1]</code>, indicates that to take course <code>0</code> you have to first take course <code>1</code>.</li>
</ul>
<p>Return <code>true</code> if you can finish all courses. Otherwise, return <code>false</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0]]
<strong>Output:</strong> true
<strong>Explanation:</strong> There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> numCourses = 2, prerequisites = [[1,0],[0,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= numCourses <= 2000</code></li>
<li><code>0 <= prerequisites.length <= 5000</code></li>
<li><code>prerequisites[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code></li>
<li>All the pairs prerequisites[i] are <strong>unique</strong>.</li>
</ul>
> [!tip]- Hint 1
>
> This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
> [!tip]- Hint 2
>
> <a href="https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/03Graphs.pdf" target="_blank">Topological Sort via DFS</a> - A great tutorial explaining the basic concepts of Topological Sort.
> [!tip]- Hint 3
>
> Topological sort could also be done via <a href="http://en.wikipedia.org/wiki/Topological_sorting#Algorithms" target="_blank">BFS</a>.
---
[submissions](https://leetcode.com/problems/course-schedule/submissions/) | [solutions](https://leetcode.com/problems/course-schedule/solutions/)
tab: 中文
<p>你这个学期必须选修 <code>numCourses</code> 门课程,记为 <code>0</code> 到 <code>numCourses - 1</code> 。</p>
<p>在选修某些课程之前需要一些先修课程。 先修课程按数组 <code>prerequisites</code> 给出,其中 <code>prerequisites[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> ,表示如果要学习课程 <code>a<sub>i</sub></code> 则 <strong>必须</strong> 先学习课程 <code>b<sub>i</sub></code><sub> </sub>。</p>
<ul>
<li>例如,先修课程对 <code>[0, 1]</code> 表示:想要学习课程 <code>0</code> ,你需要先完成课程 <code>1</code> 。</li>
</ul>
<p>请你判断是否可能完成所有课程的学习?如果可以,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
<p> </p>
<p><strong>示例 1:</strong></p>
<pre>
<strong>输入:</strong>numCourses = 2, prerequisites = [[1,0]]
<strong>输出:</strong>true
<strong>解释:</strong>总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong>numCourses = 2, prerequisites = [[1,0],[0,1]]
<strong>输出:</strong>false
<strong>解释:</strong>总共有 2 门课程。学习课程 1 之前,你需要先完成课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= numCourses <= 2000</code></li>
<li><code>0 <= prerequisites.length <= 5000</code></li>
<li><code>prerequisites[i].length == 2</code></li>
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < numCourses</code></li>
<li><code>prerequisites[i]</code> 中的所有课程对 <strong>互不相同</strong></li>
</ul>
> [!tip]- 提示 1
>
> This problem is equivalent to finding if a cycle exists in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
> [!tip]- 提示 2
>
> <a href="https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/03Graphs.pdf" target="_blank">Topological Sort via DFS</a> - A great tutorial explaining the basic concepts of Topological Sort.
> [!tip]- 提示 3
>
> Topological sort could also be done via <a href="http://en.wikipedia.org/wiki/Topological_sorting#Algorithms" target="_blank">BFS</a>.
---
[提交记录](https://leetcode.cn/problems/course-schedule/submissions/) | [题解](https://leetcode.cn/problems/course-schedule/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122