【LeetCode】207. Course Schedule (2 solutions)
Course Schedule
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, is it possible for you to finish all courses?
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 it is possible.
2, [[1,0],[0,1]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
Note:
The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
解法一:每个连通分量,只要出现回路,即说明冲突了。
回路检测如下:
存在a->b,又存在b->c,那么增加边a->c
如果新增边c->a,发现已有a->c,在矩阵中表现为对称位置为true,则存在回路。
注:数组比vector速度快。
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
bool **preG;
preG = new bool*[numCourses];
for(int i = ; i < numCourses; i ++)
preG[i] = new bool[numCourses];
for(int i = ; i < numCourses; i ++)
{
for(int j = ; j < numCourses; j ++)
preG[i][j] = false;
}
for(int i = ; i < prerequisites.size(); i ++)
{
int a = prerequisites[i].first;
int b = prerequisites[i].second;
if(preG[b][a] == true)
return false;
else
{
preG[a][b] = true;
for(int j = ; j < numCourses; j ++)
{
if(preG[j][a] == true)
preG[j][b] = true;
}
}
}
return true;
}
};

解法二:不断删除出度为0的点,如果可以逐个删除完毕,说明可以完成拓扑序,否则说明存在回路。
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> outd(numCourses, );
vector<bool> del(numCourses, false);
unordered_map<int, vector<int> > graph;
// construct reverse neighborhood graph
// graph is to decrease the out-degree of a set of vertices,
// when a certain vertice is deleted
for(int i = ; i < prerequisites.size(); i ++)
{
outd[prerequisites[i].first] ++;
graph[prerequisites[i].second].push_back(prerequisites[i].first);
}
int count = ;
while(count < numCourses)
{
int i;
for(i = ; i < numCourses; i ++)
{
if(outd[i] == && del[i] == false)
break;
}
if(i < numCourses)
{
del[i] = true; // delete
for(int j = ; j < graph[i].size(); j ++)
{// decrease the degree of vertices that links to vertice_i
outd[graph[i][j]] --;
}
count ++;
}
else
{// no vertice with 0-degree
return false;
}
}
return true;
}
};

【LeetCode】207. Course Schedule (2 solutions)的更多相关文章
- 【LeetCode】207. Course Schedule 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/course-s ...
- 【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 ...
- 【LeetCode】210. Course Schedule II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拓扑排序,BFS 拓扑排序,DFS 参考资料 日期 ...
- 【刷题-LeetCode】207. Course Schedule
Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 【LeetCode】90. Subsets II (2 solutions)
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【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】44. Wildcard Matching (2 solutions)
Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- Oracle学习笔记(6)——函数
函数的作用 方便数据的统计 处理查询结果 函数的分类 Oracle内置的系统函数 数值函数 四舍五入 ROUND ...
- [PureScript] Basic Data Constructors in PureScript
PureScript types are very extensive and we are going to experiment with type constructors and how to ...
- 曾经的超级明星类库jQuery未来也许不再会被前端程序猿追捧了!
作为火了十多年的老牌明星类库jQuery, 相信做前端的小伙伴肯定都或多或少的使用和追捧过,当然我也不例外, 作为第一个学习的js类库,我曾经也觉得它是真正的唯一, 帮助你处理恶心的浏览器CSS/JS ...
- js获取GET参数
自定义函数 /*-----------------实现1--------------------*/ function getPar(par){ //获取当前URL var local_url = d ...
- Office WPS如何在页眉页脚添加一条横线
点击样式,页眉,修改 然后设置格式,可以添加一条或者两条横线,也可以设置不同的线型 最后效果如下图所示
- SuperMap开发入门1——资源下载
前言(废话) 由于项目需要,我们将被改用超图(SuperMap)平台进行GIS开发.记忆中,我还是在学生时代使用过超图软件5.0版本,安装包只有50M,这也是超图与学校有合作关系的缘故. 在以后的学习 ...
- Python小任务 - 如何编写指定时间执行的Python小程序
我们在平时的工作中经常会遇到这样的需求,需要再某个时间点执行一段程序逻辑. 那么,在python中我们是怎么做的呢? 下面看代码: waitDesignatedTimeToRun.py import ...
- Asp.Net下通过切换CSS换皮肤
直接重写Render事件 protected override void Render(System.Web.UI.HtmlTextWriter writer) { StringWriter sw = ...
- postman添加cookie
检索cookie: 1.启动拦截器(需安装Postman Interceptor) 2.在测试部分,你可以使用responseCookies对象,他将返回一个cookie对象的数组.使用postman ...
- nyoj 119士兵杀敌(三)(线段树区间最值查询,RMQ算法)
题目119 题目信息 执行结果 本题排行 讨论区 士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军统率着N个士兵,士兵分别编号为1~N,南将军常 ...