[LeetCode] Course Schedule II

简介: Well, this problem is spiritually similar to to Course Schedule. You only need to store the nodes in the order you visit into a vector during BFS or DFS.

Well, this problem is spiritually similar to to Course Schedule. You only need to store the nodes in the order you visit into a vector during BFS or DFS. Well, for DFS, a final reversal is required.


BFS

 1 class Solution {
 2 public:
 3     vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
 4         vector<unordered_set<int>> graph = make_graph(numCourses, prerequisites);
 5         vector<int> degrees = compute_indegree(graph);
 6         queue<int> zeros;
 7         for (int i = 0; i < numCourses; i++)
 8             if (!degrees[i]) zeros.push(i);
 9         vector<int> toposort;
10         for (int i = 0; i < numCourses; i++) {
11             if (zeros.empty()) return {};
12             int zero = zeros.front();
13             zeros.pop();
14             toposort.push_back(zero);
15             for (int neigh : graph[zero]) {
16                 if (!--degrees[neigh])
17                     zeros.push(neigh);
18             }
19         }
20         return toposort;
21     }
22 private:
23     vector<unordered_set<int>> make_graph(int numCourses, vector<pair<int, int>>& prerequisites) {
24         vector<unordered_set<int>> graph(numCourses);
25         for (auto pre : prerequisites)
26             graph[pre.second].insert(pre.first);
27         return graph; 
28     }
29     vector<int> compute_indegree(vector<unordered_set<int>>& graph) {
30         vector<int> degrees(graph.size(), 0);
31         for (auto neighbors : graph)
32             for (int neigh : neighbors)
33                 degrees[neigh]++;
34         return degrees;
35     }
36 };

DFS

 1 class Solution {
 2 public:
 3     vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
 4         vector<unordered_set<int>> graph = make_graph(numCourses, prerequisites);
 5         vector<int> toposort;
 6         vector<bool> onpath(numCourses, false), visited(numCourses, false);
 7         for (int i = 0; i < numCourses; i++)
 8             if (!visited[i] && dfs(graph, i, onpath, visited, toposort))
 9                 return {};
10         reverse(toposort.begin(), toposort.end());
11         return toposort;
12     }
13 private:
14     vector<unordered_set<int>> make_graph(int numCourses, vector<pair<int, int>>& prerequisites) {
15         vector<unordered_set<int>> graph(numCourses);
16         for (auto pre : prerequisites)
17             graph[pre.second].insert(pre.first);
18         return graph;
19     }
20     bool dfs(vector<unordered_set<int>>& graph, int node, vector<bool>& onpath, vector<bool>& visited, vector<int>& toposort) { 
21         if (visited[node]) return false;
22         onpath[node] = visited[node] = true; 
23         for (int neigh : graph[node])
24             if (onpath[neigh] || dfs(graph, neigh, onpath, visited, toposort))
25                 return true;
26         toposort.push_back(node);
27         return onpath[node] = false;
28     }
29 };

 

目录
相关文章
|
索引
LeetCode 207. Course Schedule
现在你总共有 n 门课需要选,记为 0 到 n-1。 在选修某些课程之前需要一些先修课程。 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量以及它们的先决条件,判断是否可能完成所有课程的学习?
61 0
LeetCode 207. Course Schedule
[LeetCode] Course Schedule III 课程清单之三
There are n different online courses numbered from 1 to n. Each course has some duration(course length) tand closed on dth day.
1065 0
LeetCode - 207. Course Schedule
207. Course Schedule  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个有向图,判断是否存在top_sort序列.
972 0
[LeetCode] Course Schedule
As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented by prerequisites.
854 0
|
4天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
8 0
|
4天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
9 0
|
5天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
21 4
【刷题】 leetcode 面试题 08.05.递归乘法