LN : leetcode 207 Course Schedule
lc 207 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.
DFS Accepted##
这是一个非常典型的拓扑排序问题,整个问题可以总结为如何判断有向图是否有环。通过对图上的每一个点做DFS,如果都是无环的,则能证明课程安排是合理的。注意,点的状态有三种,分别是没被访问,第一次访问后以及第二次访问后。
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<set<int>> graph(numCourses);
bool cycle = false;
for (auto prev : prerequisites) {
graph[prev.second].insert(prev.first);
}
vector<int> visited(numCourses, 0);
for (int i = 0; i < numCourses; i++) {
if (cycle) return false;
if (visited[i] == 0) dfs(i, graph, visited, cycle);
}
return !cycle;
}
void dfs(int nodei, vector<set<int>> &graph, vector<int> &visited, bool &cycle) {
if (visited[nodei] == 1) {
cycle = true;
return;
}
visited[nodei] = true;
for (auto i : graph[nodei]) {
dfs(i, graph, visited, cycle);
if (cycle) return;
}
visited[nodei] = 2;
}
};
LN : leetcode 207 Course Schedule的更多相关文章
- LeetCode - 207. Course Schedule
207. Course Schedule Problem's Link ---------------------------------------------------------------- ...
- Java for LeetCode 207 Course Schedule【Medium】
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- [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 prer ...
- [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 prereq ...
- (medium)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 prer ...
- 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 p ...
- [LeetCode] 207. Course Schedule 课程表
题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...
- [leetcode]207. Course Schedule课程表
在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在). /* ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- 文件I/O和标准I/O
转载:https://blog.csdn.net/kyang_823/article/details/79496561 一.文件I/O和标准I/O文件I/O:文件I/O也称为不带缓冲的I/O(unbu ...
- dubbo安装和使用
转载:http://blog.csdn.net/zjcjava/article/details/78766095 背景 Dubbo的开源人梁飞在内部的交流会上宣布重启dubbo的维护和更新,具体PPT ...
- REST API 安全设计
REST API 安全设计 2017年04月27日 18:34:27 阅读数:1699 Rest API 的那些事儿 作者/ asterisk 在软件行业快速发展的今天,传统的软件授权已经不能足以 ...
- python搭建web server
假设你急需一个简单的Web Server,但你又不想去下载并安装那些复杂的HTTP服务程序,比方:Apache,ISS等.那么, Python 可能帮助你.使用Python能够完毕一个简单的内建 HT ...
- linux下配置LAMP开发环境,以及经常使用小细节
本来安装没什么可说到.可是在linux其中easy会出现各种各样到问题. 我安装以后导致各种问题 比方php无法正常解析,数据库无法关闭,Apache无法开启等等........ 所以搞得我比較郁闷, ...
- Ubuntu18.04系统中vi键盘输入字符不匹配
起因 今天重装了我的雷神笔记本的ubuntu18.04,不要问我为什么,我就是想复习下重装系统而已.好吧,我承认我改错文件启动不起来了. 于是我要重装jdk.maven and so on,但是当我用 ...
- linux 下查看一个进程执行路径
在linux下查看进程大家都会想到用 ps -ef|grep XXX 但是看到的不是全路径.怎么看全路径呢? 每一个进程启动之后在 /proc以下有一个于pid相应的路径 比如:ps -ef|grep ...
- Android oat文件提取转换
说明: 1.手机厂商可以修改Android源码并进行编译后再生成oat格式文件在手机上存储,比如boot-okhttp.oat,boot-framework.oat. 2.自带的apk可以调用这些模块 ...
- 2016/2/22 1、Window.document对象
1.Window.document对象 一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunme ...
- uclibc,eglibc,glibc之间的区别和联系【转】
本文转载自:https://www.crifan.com/relation_between_uclibc_glibc_eglibc/ [glibc,uclibc,eglibc的简介] 1.Glibc ...