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. django-替代为自定义的User model

    https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model Subs ...

  2. BZOJ 4084 [Sdoi2015]双旋转字符串

    题解:hash 至今不会unsigned long long 的输出 把B扔进map 找A[mid+1][lenA]在A[1][mid]中的位置 把A[1][mid]贴两遍(套路) 枚举A[mid+1 ...

  3. 【每日Scrum】第四天冲刺

    一.计划会议内容 连接数据库报错,解决问题中. 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图  

  4. Block实现代理/通知效果

    例子1:A控制器->跳转—>B控制器 , 假设想从B控制器回传数组给A控制器 实现:B控制器.h文件定义一个block参数,.m文件执行block,A控制器设置block内容 B.h文件/ ...

  5. Java之创建线程的方式三:实现Callable接口

    import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util ...

  6. Ubuntu16.04编译OpenCV3.4.7

    原文:https://www.bearoom.xyz/2019/08/20/ubuntu16-04-make-opencv3-4-7/ 一.前言 因为之前作死乱搞系统,然后就把Ubuntu的系统搞垮了 ...

  7. Paper Review: Epigenetic Landscape, Cell Differentiation 02

    I'll share another review paper about Epigenetic Landscape, it comes from Nature Review, published i ...

  8. LaunchPad(思维)

    链接:https://ac.nowcoder.com/acm/contest/3665/D来源:牛客网 题目描述 Hery is a boy with strong practical abiliti ...

  9. Codeforce 370C Mittens 巧妙数学题

    这道题目我一开始想错了,觉得只要排好序,再从头到尾把可以相互交换的进行下交换就可以了...事实证明是错的.正确的解法比较巧妙,而且写法非常好,值得学习 首先,要注意的一个规律是,假如最大的颜色数字出现 ...

  10. gradle问题

    1, my gradle version is 4.6 . in project.gradle : change dependencies { classpath 'com.android.tools ...