[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?
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.
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
if (numCourses == 0 || prerequisites == null) {
return false;
}
int[] indegree = new int[numCourses];
for (int[] pres : prerequisites) {
indegree[pres[0]] += 1;
}
int res = numCourses; Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < indegree.length; i++) {
if (indegree[i] == 0) {
queue.offer(i);
}
}
while(!queue.isEmpty()) {
Integer cur = queue.poll();
res -= 1; for (int[] pres: prerequisites) {
if (pres[1] == cur) {
// for a -> b, minus degree of a
indegree[pres[0]] -= 1;
if (indegree[pres[0]] == 0) {
queue.offer(pres[0]);
}
}
}
}
return res == 0;
}
}
[LC] 207. Course Schedule的更多相关文章
- 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
207. Course Schedule Problem's Link ---------------------------------------------------------------- ...
- 207. Course Schedule
https://blog.csdn.net/wongleetion/article/details/79433101 问题的实质就是判断一个有向图是否有环,利用入度去解决这个问题 使用bfs解决问题. ...
- [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 ...
- 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 (2 solutions)
Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some cours ...
- [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
Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...
- (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 ...
随机推荐
- repr. str, ascii in Python
repr和str a="Hello" print(str(a)) print(repr(a)) 结果: Hello 'Hello' 可以看出,repr的结果中多了左右两个引号. r ...
- org.springframework.test.context.junit4.SpringJUnit4ClassRunner
项目中有了spring-test的依赖,里面确实也有 org.springframework.test.context.junit4.SpringJUnit4ClassRunner 此类,但是项目就是 ...
- Notepad++配置
笔记来源于视频: http://baidu.iqiyi.com/watch/498601896985630918.html?page=videoMultiNeed notepad++ 有个很重要问题, ...
- Filter过滤器技术详解
前言 有这样一个常见的开发场景,我们编写一套系统,或者分析一套系统如何实现的过程中,我们肯定会发现这套系统的拦截机制.比如说京东或者淘宝之类的,存在这种拦截机制,这套拦截机制能够过滤掉哪些错误的登录注 ...
- 学生信息的添加 Java web简单项目初试(修改)
错误原因: 1.Java web 的Servlet类没有配置好,并且缺少一个 Dao类(Date Access Object通常用于操作数据库的). 2.代码的某些名称错误,导致数据库数据存储错误. ...
- UVALive 6491 You win! 状态DP
这个题目上周的对抗赛的,美国2013区域赛的题目,上次比赛真惨,就做出一道题,最多的也只做出两道,当时想把这题做出来,一直TLE. 这个题目用挂在Hunnu OJ的数据可以过,但UVALive上死活过 ...
- input框随输入的文字的多少自动调整宽度粗略版本
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Block实现代理/通知效果
例子1:A控制器->跳转—>B控制器 , 假设想从B控制器回传数组给A控制器 实现:B控制器.h文件定义一个block参数,.m文件执行block,A控制器设置block内容 B.h文件/ ...
- 2019年春PAT甲级考试
这次考试不是很理想,一道题目没能做完. 自己原因差不多三条: 1.自己实力不够,准备时间也有点仓促,自己没能做到每道题目都有清晰的思路. 2.考试的心理素质不行,因为设备原因东奔西跑浪费了挺多时间,自 ...
- faster rcnn 源码学习-------数据读入及RoIDataLayer相关模块解读
参考博客:::https://www.cnblogs.com/Dzhen/p/6845852.html 非常全面的解读参考:::https://blog.csdn.net/DaVinciL/artic ...