[Leetcode Week4]Course Schedule II
Course Schedule II题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/course-schedule-ii/description/
Description
There are a total of n courses you have to take, labeled from 0 to n - 1.
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.
For example:
2, [[1,0]]
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]
4, [[1,0],[2,0],[3,1],[3,2]]
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.
Solution
class Solution {
private:
vector<int> inDegree;
vector<bool> isFinish;
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
inDegree.resize(numCourses);
isFinish.resize(numCourses);
int i, v;
for (i = 0; i < numCourses; i++) {
inDegree[i] = 0;
isFinish[i] = false;
}
for (i = 0; i < prerequisites.size(); i++)
inDegree[prerequisites[i].first]++;
queue<int> vq;
for (i = 0; i < numCourses; i++) {
if (inDegree[i] == 0)
vq.push(i);
}
vector<int> resultVec;
if (vq.empty())
return resultVec;
while (!vq.empty()) {
v = vq.front();
vq.pop();
resultVec.push_back(v);
isFinish[v] = true;
for (i = 0; i < prerequisites.size(); i++) {
if (v == prerequisites[i].second) {
inDegree[prerequisites[i].first]--;
if (inDegree[prerequisites[i].first] == 0 && !isFinish[prerequisites[i].first])
vq.push(prerequisites[i].first);
}
}
}
if (resultVec.size() == numCourses) {
return resultVec;
}
else {
vector<int> r;
return r;
}
}
};
解题描述
这道题与Course Schedule的解法基本是一样的。我还是采用了BFS进行拓扑排序,总的来说没有什么新的坑点。
[Leetcode Week4]Course Schedule II的更多相关文章
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)
和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...
- [LeetCode] 210. Course Schedule II 课程清单之二
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- Leetcode 210 Course Schedule II
here are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prere ...
- [LeetCode] 210. Course Schedule II 课程安排II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- (medium)LeetCode 210.Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [leetcode]210. Course Schedule II课程表II
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...
- [LeetCode] 207. Course Schedule 课程安排
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- 【LeetCode】210. Course Schedule II
Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...
随机推荐
- C++学习009预处理器指令符号 # ## #@ 符号的使用
# ## #@ 符号是预处理器指令符号. 当预处理器遇到#指令符号时,会将#之后的部分用双引号括起来 当预处理去遇到##指令符号时,直接将##前后部分连接起来 当预处理器遇到#@指令符号,将#@之后的 ...
- Sublime Text 3配置 Python3 开发环境
来自 https://www.cnblogs.com/zhangqinwei/p/6886600.html Sublime Text作为一款支持多种编程语言的文本编辑神器,深受广大开发者的喜爱.通过简 ...
- jira+mysql+破解+中文+compose
1.制作docker-compose.yml 2.安装 $ docker stack deploy -c docker-compose.yml mshk_jira
- PHP全局变量局部变量
http://www.w3school.com.cn/php/php_variables.asp
- CSS设计指南之ID属性
1.用于页内导航的ID ID也可以用在页内导航连接中.下面就是一个链接,其目标是同一页的另一个位置. <a href="#bio">Biography</a> ...
- java8 增强的Iterator遍历集合元素
Iterator接口也是Java集合框架的成员,与Collection和Map两个系列的集合不一样的是Collection和Map系列主要用于充当容器的作用,而Iterator正如其名字一样是主要用于 ...
- 【BZOJ 1492】 [NOI2007]货币兑换Cash 斜率优化DP
先说一下斜率优化:这是一种经典的dp优化,是OI中利用数形结合的思想解决问题的典范,通常用于优化dp,有时候其他的一些决策优化也会用到,看待他的角度一般有两种,但均将决策看为二维坐标系上的点,并转化为 ...
- 【BZOJ 3195 】[Jxoi2012]奇怪的道路 装压dp
受惯性思维的影响自动把二进制状态认为是连与不连......... 我们这里二进制状态表示的是奇偶,这样的话我们f[i][j][k]表示的就是前i个城市用了j个边他前k个城市的奇偶状态,然后想想怎么转移 ...
- 【BZOJ 2432】 [Noi2011]兔农 矩乘+数论
这道题的暴力分还是很良心嘛~~~~~ 直接刚的话我发现本蒟蒻只会暴力,矩乘根本写不出来,然后让我们找一下规律,我们发现如果我们把这个序列在mod k的意义下摆出,并且在此过程中把值为1的的数减一,我们 ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) C
C. Little Artem and Matrix time limit per test 2 seconds memory limit per test 256 megabytes input s ...