[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] 给定课程总量以及它们的先决条件,判断是否可能完成所有课程的学习?
187 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.
1224 0
LeetCode - 207. Course Schedule
207. Course Schedule  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个有向图,判断是否存在top_sort序列.
1143 0
[LeetCode] Course Schedule
As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented by prerequisites.
990 0
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
468 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
244 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
564 2
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
472 4
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口

热门文章

最新文章