【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 ...
随机推荐
- LSTM简介以及数学推导(FULL BPTT)
http://blog.csdn.net/a635661820/article/details/45390671 前段时间看了一些关于LSTM方面的论文,一直准备记录一下学习过程的,因为其他事儿,一直 ...
- win8下Source Insight has not been installed completely问题的解决
系统:windows8 软件:Source Insight 3.5 安装后打开总是提示如下图错误,没法使用. 卸载重新安装好多次,还是不行,百度一下,终于找到方法,记录一下,方便以后查找. 解决方法: ...
- 你应该知道的Linux历史
说道linux的历史不得不说的就是unix,我们的linux就是类unix系统: 1969年第一台unix系统 贝尔实验室 1970年C语言诞生,对unix内核重新编写 system V(AT& ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(四)针对hadoop2.9.0启动执行start-all.sh出现异常:failed to launch: nice -n 0 /bin/spark-class org.apache.spark.deploy.worker.Worker
启动问题: 执行start-all.sh出现以下异常信息: failed to launch: nice -n 0 /bin/spark-class org.apache.spark.deploy.w ...
- 主成分分析(PCA)原理及推导
原文:http://blog.csdn.net/zhongkejingwang/article/details/42264479 什么是PCA? 在数据挖掘或者图像处理等领域经常会用到主成分分析,这样 ...
- create-react-app时registry的奇怪问题
用React官方给的NPM脚本 create-react-app my-app 在自动安装module的过程中,在安装registry的组件的时候莫名其妙的挂住不动了.界面显示的信息如下: fetch ...
- ASP入门(二)-创建Access数据库
通常来说,ASP程序是搭配Access数据库来使用的,因此在安装完ASP环境后,为了方便建立和管理数据库,我们还需要安装Access数据库. Access是Microsoft Office家族中的一员 ...
- 放入MP3的文件夹显示一些没用的标题,艺术家,唱片集怎么办?
原来的文件大小,类型,修改日期,创建日线都没了,怎么办? 右键点击名称,标题,艺术家那一行,在右键菜单中,去掉没用的,勾选有用的,如下图:
- LabVIEW上位机与串口通信
渊源 大一的时候,学校开了门公共选修课,叫LabVIEW编程,当时的我当然还不知道LabVIEW是啥东东,但还是选了.上课的老师是机械学院的一个副教授.他给我们展示了好几个用LabVIEW做的项目.譬 ...
- springboot项目启动多个实例的方法
我现在需要实现这样的功能:将一个服务提供者启动多个实例,下面我列出在eclipse中启动多个实例的方法: 首先看一下我的服务提供者的项目文件结构: springboot默认的配置文件是applicat ...