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.

click to show more hints.

解法一:每个连通分量,只要出现回路,即说明冲突了。

回路检测如下:

存在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)的更多相关文章

  1. 【LeetCode】207. Course Schedule 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/course-s ...

  2. 【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 ...

  3. 【LeetCode】210. Course Schedule II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拓扑排序,BFS 拓扑排序,DFS 参考资料 日期 ...

  4. 【刷题-LeetCode】207. Course Schedule

    Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...

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

  6. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

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

  8. 【LeetCode】44. Wildcard Matching (2 solutions)

    Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any ...

  9. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. VS调试快捷键

    VS调试快捷键   命令名 快捷键 说明 调试.应用代码更改 Alt + F10 启动生成操作,利用它可以通过“编辑并继续”功能应用对正在调试的代码所作的更改. 调试.自动窗口 Ctrl + D,Ct ...

  2. Apache+wsgi+flask部署

    flask自带的web server是开发用途,并不适用与发布,需要借助专业的web服务器. 配置的坑无数,Apache部署,403禁止,莫名其妙无法访问,500内部错误把我搞得崩溃了. 重点参考: ...

  3. vue版本冲突解决办法

    ERROR in Vue packages version mismatch: vue@2.1.4 vue-template-compiler@2.1.5 This may cause things ...

  4. 未能加载文件或程序集“Microsoft.SqlServer.Sqm, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91”或它的某一个依赖项。系统找不到指定的文件。 (SqlMgmt)

    解决方法: Copy this file "Microsoft.SqlServer.Sqm.dll" in the forder "C:/Program Files/Mi ...

  5. 用 CSS 实现元素垂直居中,有哪些好的方案?

    1.不知道自己高度和父容器高度的情况下, 利用绝对定位只需要以下三行: parentElement{ position:relative; } childElement{ position: abso ...

  6. JavaScript 将行结构数据转化为树形结构,可提供给常用的tree插件直接使用(高效转化方案)

    前台接收到的数据格式 var rows=[{ parent: 'root', id: 'DC', title: '集团' }, { parent: 'DC', id: '01', title: '上海 ...

  7. iOS社交app技术合伙人笔试题

    理想状况当然是找到有管理能力且还在写代码的架构师了,然而假设有这种人,他自己能发起项目了.你要拉上他还真难.满足一定条件就可以了,别追求完美. 还有比这更完美的吗?请生产这种机器人: 性格开朗(开朗≠ ...

  8. 解剖 CPU

    http://www.ruanyifeng.com/blog/2010/11/cpu_autopsy.html 有一个瑞典 Lund 大学物理学博士生,就真的这么干了,还把照片放到网上.我们知道,CP ...

  9. Springboot项目启动报错,提示Cannot determine embedded database driver class for database type NONE

    我在springboot项目里面引入了数据库的配置: <dependency> <groupId>org.mybatis.spring.boot</groupId> ...

  10. sed awk tr等文本处理命令

    指定行范围替换: sed -i "520,950s/\(.*\)\(HOST_CMD_.*\)\(,\)/\1{ \2, \"\2\" },/g" hostCm ...