Course Schedule II
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:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- This problem is equivalent to finding the topological order in a directed graph. If a cycle exists, no topological ordering exists and therefore it will be impossible to take all courses.
- Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
- Topological sort could also be done via BFS.
紧接着上一题,同样是拓扑序问题。
class Solution{
public:
vector<int> findOrder(int numCourses,vector<pair<int,int>>& prerequisites){
vector<int> res;
vector<int> in_degree(numCourses,);
vector<vector<int>> graph(numCourses);
for(auto p : prerequisites){
in_degree[p.first]++;
graph[p.second].push_back(p.first);
}
queue<int> q;
for(int i=;i<numCourses;i++){
if(in_degree[i] == )
q.push(i);
}
while(!q.empty()){
int cur = q.front();
q.pop();
res.push_back(cur);
for(auto it = graph[cur].begin();it != graph[cur].end();it++){
if(--in_degree[*it] == )
q.push(*it);
}
}
if(res.size() == numCourses)
return res;
else
return vector<int>();
}
};
Course Schedule II的更多相关文章
- 【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 ...
- [Leetcode Week4]Course Schedule II
Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...
- 【刷题-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 cour ...
- [LeetCode] 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 ...
- 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 Course Schedule II
原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 题目: There are a total of n courses you hav ...
- FB面经prepare: task schedule II
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...
- 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 ...
- 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 ...
- Course Schedule II 解答
Question There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may ...
随机推荐
- centOS 6.7 中安装matlab R2014b
参考资料: [1] http://www.centoscn.com/image-text/config/2014/1222/4354.html 系统: centOS 6.7 2.6.32-573.el ...
- makefile 学习网站
http://blog.csdn.net/ruglcc/article/details/7814546/#t30
- 解析提高PHP执行效率的50个技巧
1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的”函数”(译注:PHP手 ...
- XlFileFormat Enumeration
https://msdn.microsoft.com/zh-cn/library/office/ff198017.aspx
- μC/OS-Ⅲ系统的任务切换和任务调度
一.任务切换 在操作系统中当任务需要从一个任务切换到另外一个任务时,要将当前任务的现场保存到当前任务的堆栈中(当前任务现场主要指CPU相关寄存器),然后回复新任务的现场并执行新任务.这个叫做上下文切换 ...
- 计算机网络(9)-----TCP可靠传输的实现
TCP可靠传输的实现 以字节为单位的滑动窗口 滑动窗口的滑动是以字节为单位的,发送方A和接收方B在TCP三次握手的前两次握手时协商好了发送窗口和接受窗口的大小,发送方A根据B发送来的确认连接报文中标明 ...
- Mvc4学习笔记一(Ajax.ActionLink)
<style type="text/css"> #left {width:200px; min-height:500px;border:1px solid #ccc;f ...
- 基于Flask的Web应用部署到SAE上遇到的问题
我的应用底层数据库用的是MySQL,利用Flask-SQLALchemy实现接口操作.我遇到的问题是: 在我把代码部署到SAE上后,当数据向数据库insert的时候总是出现“2006,MySQL ha ...
- zookeeper集群安装与配置
转自: http://my.oschina.net/u/2377453/blog/464739 1.解压zookeeper 2.在$ZOOKEEPER_HOME/conf下创建zoo.cfg文件(参考 ...
- STM32——CAN通讯实现
CAN通讯的实现步骤: 1.CAN初始化,其中包括:a.配置CAN时钟,配置IO: b.使能CAN中断向量: c.CAN硬件寄存器配置初始化: d.过滤器初始化: e.打开CAN中断. 2.CAN发送 ...