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.

Hints:
    1. 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.
    2. Topological Sort via DFS - A great video tutorial (21 minutes) on Coursera explaining the basic concepts of Topological Sort.
    3. Topological sort could also be done via BFS.

207. Course Schedule的拓展,解法与其类似,这里要按顺序添加要完成的课程。拓扑排序,最后加一步判断是否存在环,如果存在环则返回空集合。使用BFS和DFS均可,区别在于是按照入度还是出度来考虑。

C++:

class Solution {
public:
/**
* 完成所有的课程的顺序
* bfs拓扑排序
* @param numCourses 课程数量
* @param prerequisites 课程先序关系
* @return 能完成返回课程顺序,否则返回空
*/
vector<int> findOrder(int numCourses, vector<pair<int, int> >& prerequisites) {
vector<int> heads(numCourses, -1), degree(numCourses, 0), points, args;
pair<int, int> p;
int from, to, count = 0, len = prerequisites.size(); /* 构造有向图,邻接表 */
for (int i = 0; i < len; ++i) {
p = prerequisites[i];
from = p.second;
to = p.first;
++degree[to];
args.push_back(heads[from]);
points.push_back(to);
heads[from] = count++;
} /* bfs拓扑排序,依次移除入度为0的点 */
vector<int> ret;
queue<int> q;
for (int i = 0; i < numCourses; ++i)
if (degree[i] == 0) q.push(i);
while (!q.empty()) {
from = q.front();
ret.push_back(from); // 课程完成,添加到结果集中
q.pop();
to = heads[from];
while (to != -1) {
if(--degree[points[to]] == 0) q.push(points[to]);
to = args[to];
}
} /* 判定是否所有的点入度都为0,若是则不存在环,否则存在环 */
for (int i = 0; i < numCourses; ++i)
if (degree[i] > 0) {
ret.clear();
break;
} return ret;
}
};

C++:DFS

class Solution {
public:
/**
* 完成所有的课程的顺序
* dfs拓扑排序
* @param numCourses 课程数量
* @param prerequisites 课程先序关系
* @return 能完成返回课程顺序,否则返回空
*/
vector<int> findOrder(int numCourses, vector<pair<int, int> >& prerequisites) {
vector<int> heads(numCourses, -1), degree(numCourses, 0), points, args;
pair<int, int> p;
int from, to, count = 0, len = prerequisites.size(); /* 构造有向图,邻接表 */
for (int i = 0; i < len; ++i) {
p = prerequisites[i];
from = p.second;
to = p.first;
++degree[from];
args.push_back(heads[to]);
points.push_back(from);
heads[to] = count++;
} /* dfs拓扑排序,依次移除出度为0的点 */
vector<int> ret;
queue<int> q;
for (int i = 0; i < numCourses; ++i)
if (degree[i] == 0) q.push(i);
while (!q.empty()) {
to = q.front();
ret.push_back(to); // 课程完成添加到结果集中
q.pop();
from = heads[to];
while (from != -1) {
if(--degree[points[from]] == 0) q.push(points[from]);
from = args[from];
}
} /* 判定是否所有的点入度都为0,若是则不存在环,否则存在环 */
for (int i = 0; i < numCourses; ++i)
if (degree[i] > 0) {
ret.clear();
break;
} /* 逆序 */
reverse(ret.begin(), ret.end());
return ret;
}
};

  

  

[LeetCode] 210. Course Schedule II 课程安排II的更多相关文章

  1. 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 ...

  2. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. (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 ...

  7. 48.Course Schedule(课程安排)

    Level:   Medium 题目描述: There are a total of n courses you have to take, labeled from 0 to n-1. Some c ...

  8. [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 ...

  9. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

随机推荐

  1. (安全之路)从头开始学python编程之文件操作

    0x00 python学习路径 b站(哔哩哔哩)视频,w3cschool(详情百度),官方文档,各大群内获取资料等等方式 0x01 python的学习要点 open()函数:有两个参数,文件名跟模式, ...

  2. Cisco网络模拟器踩坑记录

    1.在我们新建一个拓扑图的时候,选择设备之间的连线种类有时会导致线路不通的情况(两个端口上为红色点)这时候建议拆除这条线后选择闪电标记 的万能线帮助我们自动创建连线(这时就能根据它显示的线条种类得知应 ...

  3. mac 使用 brew 安装 nginx 及各种命令

    一.安装 brew install nginx 或 sudo brew install nginx 二.启动 brew services start nginx 或 sudo brew service ...

  4. linux Crontab定时备份项目案例

    首先先写好备份的脚本(拷贝的命令) #bash/bin cd /finance/tomcat8-finance/wtpwebapps tar -czf /finance/webapp_backup/* ...

  5. fastjson异常(字符串集合转成字符串数组)

    我是在项目中,因为受到一个string类型的list集合,然后需要把这个字符串发送给前端,进行解析. 但是前端收到的是一个字符串,不能进行解析. 所以采用 ArrayUtils.clone(JSONO ...

  6. vue 中 axios 使用

    前言 在对接接口的时候时常会有传参问题调调试试很多,是 JSON.From Data还是 URL 传参,没有搞清楚就浪费很多时间. 本文中就结合 axios 来说明这些的区别,以便在以后工作更好对接. ...

  7. Alpha冲刺(8/10)——2019.5.1

    所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(8/10)--2019.5.1 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万 ...

  8. mongodb 简单使用说明

    首先安装  mongodb软件地址 https://www.mongodb.org/downloads#production: 然后在 mongodb安装目录下找到bin 文件夹进去 在它的位置上按下 ...

  9. django-缓存django-redis

    https://django-redis-chs.readthedocs.io/zh_CN/latest/ 安装 django-redis 最简单的方法就是用 pip : pip install dj ...

  10. mybatis连接mysql查询时报Cannot convert value '0000-00-00 00:00:00' from column 10 to TIMESTAMP

    今天在学习mybatis框架的时候遇到了一个问题:查询用户表的时候报 Cannot convert value '0000-00-00 00:00:00' from column 10 to TIME ...