48.Course Schedule(课程安排)
####Level:
Medium
####题目描述:
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?
Example 1:
Input: 2, [[1,0]]
Output: true
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0. So it is possible.
Example 2:
Input: 2, [[1,0],[0,1]]
Output: false
Explanation: 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.
- You may assume that there are no duplicate edges in the input prerequisites.
####思路分析:
这题是拓扑排序一道应用,首先,我们应该统计每个点的入度,然后根据拓扑排序的规则,先删去入度为0的点,直到最后点的个数为0,则是正确的排课。
####代码:
public class Solution{
public boolean canFinish(int coursenum,int[][]prerequisites){
int []indegree=new int [coursenum];//记录每门课的入度。
for(int []pair:prerequisites){
indegree[pair[0]]++; //统计每门课的入度
}
Queue<Integer>q=new LinkedList<>(); //存放入度为0的课程,准备删除
for(int i=0;i<coursenum;i++){
if(indegree[i]==0)
q.offer(i);
}
while(!q.isEmpty()){
int key=q.poll();//删除一个入度为0的课程
coursenum--;//课程数减1
for(int[] pair:prerequisites){
if(pair[1]==key){//因为删除了节点,那么和这个节点相连的节点入度减一。
indegree[pair[0]]--;
if(indegree[pair[0]]==0)//如果减一后,这个节点的入度为0,则加入删除队列
q.offer(pair[0]);
}
}
}
return coursenum==0; //如果最终都被删除,则满足排课顺序
}
}
48.Course Schedule(课程安排)的更多相关文章
- [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 ...
- 01Mybatis_课程安排
课程安排: mybatis和springmvc通过订单商品 案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开发)问题总结 mybatis框架原理 (掌握) m ...
- 中科院 2014年GCT考前辅导课程安排
: 2014年GCT考前辅导课程安排 发布时间: 2014-07-14 阅读次数:1225 默认字体 9pt ...
- SpringMVC由浅入深day02_1课程安排_2包装类型pojo参数绑定_3集合类型绑定
springmvc第二天 高级知识 复习: springmvc框架: DispatcherServlet前端控制器:接收request,进行response HandlerMapping处理器映射器: ...
- 01_Python 基础课程安排
Python 基础课程安排 目标 明确基础班课程内容 课程清单 序号 内容 目标 01 Linux 基础 让大家对 Ubuntu 的使用从很 陌生 达到 灵活操作 02 Python 基础 涵盖 Py ...
- mybatis由浅入深day01_1课程安排_2对原生态jdbc程序中问题总结
mybatis 第一天 mybatis的基础知识 1 课程安排: mybatis和springmvc通过订单商品 案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开 ...
- Linux:课程安排、Linux简介、虚拟机安装、课前准备(常用设置和操作)
一.课程安排 1)Linux 的作用 商业服务器基本上都是 Linux: 开源软件都先支持 Linux: 大数据分析.机器学习首先选 Linux: 整个互联网地基靠Linux撑起来: Linux 系统 ...
- cogs——644. 课程安排问题
644. 课程安排问题 ★ 输入文件:curriculum.in 输出文件:curriculum.out 简单对比时间限制:1 s 内存限制:128 MB 问题描述 一个软件专业的学生 ...
- [LeetCode] 210. Course Schedule II 课程安排II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- JavaScript中的map()函数
概述Array.map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,同时不会改变原来的数组. 用法 Array.map(callback); 示例 //简单数组 const ...
- redis限流器的设计
1.定义注解 import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java. ...
- TMS320C6455BCTZA 原厂订购 原装正品
作为一家科研公司,保证芯片的原厂品质和正规采购渠道是科学严谨的研发工作中重要的一环,更是保证研发产品可靠.稳定的基础.而研发中所遇到的各种不可预测的情况更是每个工程师向技术的山峰攀登中时会遇到的各种难 ...
- Linux就该这么学07学习笔记
参考链接:https://www.linuxprobe.com/chapter-07.html RAID磁盘冗余阵列 RAID 0 RAID 0技术把多块物理硬盘设备(至少两块)通过硬件或软件的方式串 ...
- maven clean后 编译报错
<plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <config ...
- winform 自定义控件属性在属性面板中显示
Jan.David Nothing is impossible, the word itself says 'I'm possible'!" — Audrey Hepburn winform ...
- CF 36E Two Paths
传送门 真实的自闭= =+ 考试的时候老师明明说了可以路径为空T^T 然后光荣的挂掉了 20分的链[明明是最送分的] 上来就看出来欧拉回路了嘛 然后思考了一下大概奇点配个对 删一条简单路径剩下的跑欧拉 ...
- IncepText算法笔记
源头:场景文本经常受到aspect ratio, scal, and orientation的影响 为了解决这个问题,提出了inceptext,主要贡献在于添加了inception-text modu ...
- SSH配置与修改
ssh文件路径:/etc/ssh/ ssh的日志文件:/var/log/secure 端口修改:./sshd_config 服务启停: service sshd start/stop/restart ...
- threeJS射线拾取机制及案例
前言 在浏览器中浏览三维图形的时候,有时想要与三维图形之间做一些点击事件和交互操作,其中比较常用的一个解决方案就是使用Raycaster对象来实现(射线拾取). 基础知识 世界坐标系:webGL中,世 ...