【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 ...
随机推荐
- 层叠顺序与堆栈上下文、font-family字体定义顺序的
1.层叠顺序与堆栈上下文 z-index 看上去其实很简单,根据 z-index 的高低决定层叠的优先级,实则深入进去,会发现内有乾坤. 问题背景:拥有共同父容器的两个 DIV 重叠在一起,是 dis ...
- Android -- Handling back button press Inside Fragments
干货(1) 首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPressed(),所有BackHandledFragment的子类在onBackPressed方法 ...
- Android -- 状态栏高度
干货 Class<?> c = null; Object obj = null; Field field = null; int x = 0, sbar = 0; try { c = Cl ...
- Java 解决 servlet 接收参数中文乱码问题
方法一: 接收到的参数进行如下操作[不建议]: String tmp = new String(type.getBytes("iso-8859-1"), "utf-8&q ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(十八)ES6.2.2 增删改查基本操作
#文档元数据 一个文档不仅仅包含它的数据 ,也包含 元数据 —— 有关 文档的信息. 三个必须的元数据元素如下:## _index 文档在哪存放 ## _type 文档表示的对象类别 ## ...
- (转)HLSL,函数列表
中文列表 函数名 说明 abs 计算输入值的绝对值. acos 返回输入值反余弦值. all 测试非0值. any 测试输入值中的任何非零值. asin 返回输入值的反正弦值. atan 返回输入值的 ...
- scikit-learn工具学习 - random,mgrid,np.r_ ,np.c_, scatter, axis, pcolormesh, contour, decision_function
yuanwen: http://blog.csdn.net/crossky_jing/article/details/49466127 scikit-learn 练习题 题目:Try classify ...
- 【nodejs】FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
当使用大批量(>100)的SQL进行MySql数据库插值任务时,会发生以下错误: 总计将有371579条数据将被插入数据库 开始插入DB <--- Last few GCs ---> ...
- VC++导出具有命名空间的函数
问题现象 原因分析 解决的方法 1 问题现象 导出具有命名空间的函数和类.源码例如以下: 头文件MiniMFC.h namespace MiniMFC { __declspec(dllexport) ...
- Spring+hibernate+struts错题集
1.严重: Exception starting filter struts2 java.lang.ClassNotFoundException: org.apache.struts2.dispatc ...