【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 ...
随机推荐
- C++类中的访问权限问题
纠结的东西: private,public,protected方法的访问范围.(public继承下)private: 只能由该类中的函数.其友元函数访问,不能被任何其他访问,该类的对象也不能访问. p ...
- Laravel 5 中使用 JWT(Json Web Token) 实现基于API的用户认证
在JavaScript前端技术大行其道的今天,我们通常只需在后台构建API提供给前端调用,并且后端仅仅设计为给前端移动App调用.用户认证是Web应用的重要组成部分,基于API的用户认证有两个最佳解决 ...
- 【Kafka】Kafka为什么要加入分区的概念
Kafka为什么要加入分区的概念 kafka 分区 作用_百度搜索 (1 封私信)kafka中的topic为什么要进行分区? - 知乎 Kafka安装版本选择 Apache Kafka nc使用 n ...
- 转: wireshark的使用说明
原创者:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html from: https://mp.weixin.qq.com/ ...
- pycharm下设置自己的模板
在File---settings---File and Code Templates---Python script 脚本里添加: #!usr/bin/env python #-*- coding:u ...
- 【Python】安装geocoder
C:\Users\horn1\Desktop\python\49-geo>pip install geocoder Collecting geocoder Downloading https:/ ...
- MATLAB中的集合运算
matlab里关于集合运算和二进制数的运算的函数 intersect:集合交集ismember :是否集合中元素setdiff :集合差集setxor :集合异或(不在交集中的元素)union :两个 ...
- Android 演示 ViewPager
本文内容 环境 项目结构 演示 1:PagerTitleStrip 演示 2:PagerTabStrip 演示 3:ViewPager 和动态 Fragment 下载 Demo 环境 Windows ...
- 微信小程序 - 自定义swiper dots样式(非组件)
自定义须知: :组件内无法使用自定义样式变化,需要自定义 :原理就是利用swiper change事件与下标index匹配,显示不同的样式 swiper组件须知: :一旦swiper用于组件化,就 ...
- 使用Spring框架入门三:基于XML配置的AOP的使用
一.引入Jar包 <!--测试1使用--> <dependency> <groupId>org.springframework</groupId> &l ...