Python 刷Leetcode题库,顺带学英语单词(47)

简介: Python 刷Leetcode题库,顺带学英语单词(47)

Course Schedule


There are a total of n courses you have to take, labeled from 0 to n-1 .   [#207]

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]


Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?


   Example 1:

Input: 2, [[1,0]]
    Output: true
    Explanation: There are a total of 2 courses to take.
    To take course 1 you should have finished course 0. So it is possible.
    Example 2:
    Input: 2, [[1,0],[0,1]]
    Output: false

   Explanation: 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.


Note:


1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.


2. You may assume that there are no duplicate edges in the input prerequisites.



Course Schedule II


There are a total of n courses you have to take, labeled from 0 to n-1 .    [#210]

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]


Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.


   Example 1:

 

Input: 2, [[1,0]]
    Output: [0,1]
    Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1] .
    Example 2:
    Input: 4, [[1,0],[2,0],[3,1],[3,2]]
    Output: [0,1,2,3] or [0,2,1,3]

   Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.

   So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3] .



Note:


1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.


2. You may assume that there are no duplicate edges in the input prerequisites.



目录
打赏
0
0
0
0
74
分享
相关文章
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
这篇文章介绍了一个Python脚本,用于统计TXT或JSON文件中特定单词的出现次数。它包含两个函数,分别处理文本和JSON文件,并通过命令行参数接收文件路径、目标单词和文件格式。文章还提供了代码逻辑的解释和示例用法。
84 0
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
|
5月前
Leetcode(最后一个单词长度)
这篇文章介绍了两种解决LeetCode第58题的方法,即计算给定字符串中最后一个单词的长度,方法包括翻转字符串和逆向遍历统计。
36 0
|
5月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
39 0
Python编写单词复习小程序
Python编写单词复习小程序
49 0
LeetCode第58题最后一个单词的长度
LeetCode第58题"最后一个单词的长度"的解题方法,通过从字符串末尾向前遍历并计数非空格字符,直接得出最后一个单词的长度。
LeetCode第58题最后一个单词的长度
|
7月前
|
【Leetcode刷题Python】1467. 两个盒子中球的颜色数相同的概率
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
96 0
|
7月前
|
【Leetcode刷题Python】50. Pow(x, n)
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
51 1
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
167 2
|
7月前
|
【Leetcode刷题Python】73. 矩阵置零
本文介绍了LeetCode第73题的解法,题目要求在给定矩阵中将所有值为0的元素所在的行和列全部置为0,并提供了一种原地算法的Python实现。
65 0
【Leetcode刷题Python】73. 矩阵置零
|
7月前
|
【Leetcode刷题Python】LeetCode 478. 在圆内随机生成点
本文介绍了LeetCode 478题的解法,题目要求在给定圆的半径和圆心位置的情况下实现在圆内均匀随机生成点的功能,并提供了Python的实现代码。
55 1

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等