[leetcode]207. Course Schedule课程表
在一个有向图中,每次找到一个没有前驱节点的节点(也就是入度为0的节点),然后把它指向其他节点的边都去掉,重复这个过程(BFS),直到所有节点已被找到,或者没有符合条件的节点(如果图中有环存在)。
/*
思路就是平时学习的过程,每次都学习一个最先导课程(特征就是该课程没有先导课程),学完所有课程后就成功
如果在某次遍历是发现所有课程都有先导课程,就失败退出。
用一个数组记录每个课程都有几个先导课程,用多个hashset(BFS的数据记录结构)记录该课程的后续课程有哪些,
每次学一个课程,就把该课程的后续课程的先导课程-1
*/ public boolean canFinish(int numCourses, int[][] prerequisites){
List<Set> list = new ArrayList<>();
for (int i = 0; i < numCourses; i++) {
list.add(new HashSet<Integer>());
}
//记录每个课程的后续课程
for (int i = 0; i < prerequisites.length; i++) {
list.get(prerequisites[i][1]).add(prerequisites[i][0]);
}
int[] pre = new int[numCourses];
//记录每个课程的先导课程个数
for (int i = 0; i < numCourses; i++) {
Set set = list.get(i);
Iterator<Integer> iter = set.iterator();
while (iter.hasNext())
pre[iter.next()]++;
}
//下面开始BFS
for (int i = 0; i < numCourses; i++) {
int j = 0;
//寻找最先导课程
for (; j < numCourses; j++) {
if (pre[j]==0) break;
}
//无先导课程,有环
if (j==numCourses) return false;
//已经学习过的置-1
pre[j]=-1;
// bfs过程,把后续课程的
Set set = list.get(j);
Iterator<Integer> iter = set.iterator();
while (iter.hasNext())
pre[iter.next()]--;
}
return true;
}
[leetcode]207. Course Schedule课程表的更多相关文章
- [LeetCode] 207. Course Schedule 课程表
题目: 分析: 这是一道典型的拓扑排序问题.那么何为拓扑排序? 拓扑排序: 有三件事情A,B,C要完成,A随时可以完成,但B和C只有A完成之后才可完成,那么拓扑排序可以为A>B>C或A&g ...
- 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 ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- [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 ...
- 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 ...
随机推荐
- 【译】理解Rust中的局部移动
原文标题:Understanding Partial Moves in Rust 原文链接:https://whileydave.com/2020/11/30/understanding-partia ...
- 团队 Gitee 实战训练
这个课程属于 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs.com/campus/fzzcxy ...
- 卡耐基梅隆大学(CMU)元学习和元强化学习课程 | Elements of Meta-Learning
Goals for the lecture: Introduction & overview of the key methods and developments. [Good starti ...
- 20200221_python虚拟环境在Windows下安装配置_virtualenv不是内部或外部命令也不是可运行的程序或批处理文件
1. 使用管理员启动命令行; 2. 安装虚拟环境 a) .\pip install virtualenv -i https://pypi.douban.com/simple/ b) ...
- 关于建立老猿Python研学群的公告
3个月前有人建议老猿建立一个Python学习交流群,老猿自己学习Python也没多久,因此没有考虑这个事情,最近又有几个朋友在请我建立这样一个群,犹豫再三,老猿决定还是答应了,因为最近关注老猿Pyth ...
- PyQt(Python+Qt)学习随笔:QTreeView树形视图的expandsOnDoubleClick属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的expandsOnDoubleClick属性用于控制鼠标双击是否展开或折 ...
- PyQt学习随笔:QStandardItemModel使用注意事项
老猿Python博文目录 老猿Python博客地址 在使用QStandardItemModel或其派生类作为view对象的数据存储时,有如下几点需要注意: 1.如果是多行多列的数据存储,对应视图如果没 ...
- Azure Cosmos DB (五) .Net Core 控制台应用
一,引言 之前在讲Azure CosmosDB Core(SQL)核心的时候,使用了EF Core 的方式,引用了 "Microsoft.EntityFrameworkCore.Cosmos ...
- 敏捷开发(Scrum)与敏捷测试
1.敏捷测试流程和传统测试流程 软件测试是贯穿整个软件开发生命周期.对软件产品(包括阶段性产品)进行验证和确认的活动过程,也是对软件产品质量持续的评估过程,其目的是尽快尽早地发现在软件产品(包括阶段性 ...
- pytorch 实践中遇到的问题
1. SGD训练时,初始化学习率为0.05时,loss出现了 nan (百度: pytorch loss nan, 但是目前暂未看懂解释,大概是loss出现了inf,学习率偏大?)