【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 ...
随机推荐
- python3 文件及文件夹路径相关
1. #返回当前文件所在的目录 currentDir = path.dirname(__file__) # __file__ 为当前文件 2.获得某个路径的父级目录: parent_path = os ...
- [转]0.python:scikit-learn基本用法
感谢百小度治哥,该文原地址:here 经Edwin Chen的推荐,认识了scikit-learn这个非常强大的python机器学习工具包.这个帖子作为笔记.(其实都没有笔记的意义,因为他家文档做的太 ...
- 下周二推出“音视频技术WebRTC初探”公开课,欢迎捧场!
下周二推出"音视频技术WebRTC初探"公开课,欢迎捧场! 公开课课程链接:http://edu.csdn.net/huiyiCourse/detail/90 课程的解说资料 ...
- [Algorithm] Check for balanced parentheses using stack
Algorithm or program to check for balanced parentheses in an expression using stack data structure. ...
- 【算法】插入排序(Insertion Sort)
(PS:内容参考MIT算法导论) 插入排序(Insertion Sort): 适用于数目较少的元素排序 伪代码(Pseudocode): 例子(Example): 符号(notation): 时间复杂 ...
- Camtasia Studio CamStudio如何不录制鼠标
在录制的小窗口中,点击Effects-Options,然后Cursor里面取消勾选Make cursor effects 可能会报错说请选择有效的声音文件 在Sound里面选择一个有效的目录,不能 ...
- android中XRecyclerView控件的使用
控件的地址:https://github.com/XRecyclerView/XRecyclerView XRecyclerView控件是一个加强版的RecyclerView,可以很方便的实现下拉刷新 ...
- C#常用特性
1.服务器控件ID .NET服务器控件有三个ID,即:ID,ClientID和UniqueID ID为服务器控件的服务器标示,在服务器代码中通过ID来唯一区分服务器控件: ClientID是服务器控件 ...
- LoadRunner基于HTML-based script和URL-based script方式录制的区别和各自的使用场景
一.区别: 为了更加直观的区别这两种录制方式,我们可以分别使用这两种方式录制同一场景(打开百度首页)然后进行对比,录制的代码如下: HTML-based script方式打开百度首页: Action( ...
- 【转】Android Studio开发应用桌面出现两个或多个图标
原文链接:http://blog.csdn.net/jia635/article/details/78259699 解决办法: 查找的是不是自己的AndroidManifest中 多个Activity ...