[LeetCode] 210. Course Schedule II
来源:互联网 发布:淘宝一颗钻要刷多少单 编辑:程序博客网 时间:2024/05/29 17:53
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.
You may assume that there are no duplicate edges in the input prerequisites.
click to show more hints.
Hints:
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<vector<int>> adjs(numCourses); for (auto& prerequisite : prerequisites) adjs[prerequisite.second].push_back(prerequisite.first); HasCycle = false; marked.resize(numCourses, false); OnStack.resize(numCourses, false); for (int i = 0; i < numCourses; i++) if (!marked[i]) DFS(adjs, i); return HasCycle ? vector<int>() : vector<int>(res.rbegin(), res.rend()); }private: bool HasCycle; vector<int> marked; vector<int> OnStack; vector<int> res; void DFS(vector<vector<int>>& adjs, int v) { OnStack[v] = true; marked[v] = true; for (auto w : adjs[v]) { if (HasCycle) return; else if (!marked[w]) DFS(adjs, w); else if (OnStack[w]) HasCycle = true; } res.push_back(v); OnStack[v] = false; }};
class Solution {public: vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) { vector<vector<int>> adjs(numCourses); vector<int> InDeg(numCourses, 0); for (auto& prerequisite : prerequisites) { InDeg[prerequisite.first]++; adjs[prerequisite.second].push_back(prerequisite.first); } queue<int> InDegZeroQueue; for (int i = 0; i < numCourses; i++) { if (InDeg[i] == 0) InDegZeroQueue.push(i); } vector<int> res; while (!InDegZeroQueue.empty()) { int v = InDegZeroQueue.front(); InDegZeroQueue.pop(); res.push_back(v); for (auto w : adjs[v]) { if (--InDeg[w] == 0) InDegZeroQueue.push(w); } } return res.size() == numCourses ? res : vector<int>(); }};
- [leetcode] 210.Course Schedule II
- Leetcode 210. Course Schedule II
- 210. Course Schedule II LeetCode
- leetcode 210. Course Schedule II
- LeetCode *** 210. Course Schedule II
- leetcode.210. Course Schedule II
- LeetCode 210. Course Schedule II
- 【LeetCode】210. Course Schedule II
- Leetcode 210. Course Schedule II
- LeetCode 210. Course Schedule II
- Leetcode 210. Course Schedule II
- [LeetCode]210. Course Schedule II
- leetcode 210. Course Schedule II
- LeetCode 210. Course Schedule II
- LeetCode 210. Course Schedule II
- 【LeetCode】210. Course Schedule II
- [leetcode]210. Course Schedule II
- leetcode 210. Course Schedule II
- eclipse官网下载介绍
- tensorflow的类、变量和函数讲解
- shell中的eval
- Android使用okhttpfinal+gson实现网络请求
- Maven之(六)setting.xml配置文件详解
- [LeetCode] 210. Course Schedule II
- Http协议
- 半年总结之自古逢秋悲寂寥 我言秋日胜春朝
- Android推荐一款很不错的刷新控件——既简单又好用
- 堆排序【数组实现】
- git工具操作add to index 无效解决
- 百度地图api实现的定位,导航,附近搜索
- windows tomcat 多域名配置
- loadrunner 安装及汉化