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的更多相关文章

  1. LN : leetcode 207 Course Schedule

    lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...

  2. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  3. 207. Course Schedule

    https://blog.csdn.net/wongleetion/article/details/79433101 问题的实质就是判断一个有向图是否有环,利用入度去解决这个问题 使用bfs解决问题. ...

  4. [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 ...

  5. 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 ...

  6. 【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 ...

  7. [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 ...

  8. 【刷题-LeetCode】207. Course Schedule

    Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...

  9. (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 ...

随机推荐

  1. 7.CSRF攻击和文件上传漏洞攻击

    一.CSRF攻击及防范措施 1.概念 请求来源于其他网站,请求并不是用户的意愿,而是伪造的请求,诱导用户发起的请求 2.场景 攻击者盗用了你的身份,以你的名义发送恶意请求.CSRF能够做的事情包括:以 ...

  2. HTML与CSS结合的四种方式

    HTML与CSS结合的四种方式: 方式一:每个标签加一个属性: 例如:<div style="background-color:red; color: green"> ...

  3. hdu 5147 Sequence II【树状数组/线段树】

    Sequence IITime Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  4. outlook 2013邮件在服务器保留副本

    用outlook2013来收邮件确实是比较方便,但是它收邮件默认设置是:当outlook2013将在线邮箱的邮件下载至本机计算机之后,它就会删除在线邮箱中的邮件.不知道是不是以前邮箱容量比较小,所以要 ...

  5. [极客大挑战 2019]Http

    0x00知识点 了解HTTP协议,使用bp伪造. 0x01 解题 首先查看源代码,找到Secret.php 访问 使用bp查看 提示我们需要来自该网址,直接改header头信息即可,我们可以通过使用r ...

  6. vue项目 首页开发 part3

    da当拖动图标时候,只有上部分可以,下部分无响应 swiper 为根页面引用,其中的css为独立,点击swiper标签可以看见其包裹区域只有部分 那么需要修改 就需要穿透样式 外部  >> ...

  7. 快速排序&基数排序

    //快速排序 #include<stdio.h> void QuickSort(int R[],int low,int high) { int i=low,j=high; int pivo ...

  8. JAVA初学者——算数运算符

    Hello!大家好,我是浩宇大熊猫,又是学习java的一天,开开森森~ 运算符:也就是对常量或者变量进行操作的符号 表达式:用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式,不同的 ...

  9. 重载(overloading)和重写@Override

    一.重写:@Override 定义:字类方法覆盖父类方法,通俗来说就是方法里面的内容可以不一样,其他都一样. (1)必须保证权限大于等于父类的权限public>protetcted>默认& ...

  10. MySQL的InnoDB的幻读问题

    MySQL InnoDB事务的隔离级别有四级,默认是“可重复读”(REPEATABLE READ). 未提交读(READ UNCOMMITTED).另一个事务修改了数据,但尚未提交,而本事务中的SEL ...