[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 ...
随机推荐
- c#textBox控件限制只允许输入数字及小数点,是否为空
c#textBox控件限制只允许输入数字及小数点 转载 //判断按键是不是要输入的类型. if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) ...
- arp攻击 (可查看同一局域网他人手机照片)
国家法律一定要遵守,知识要用在对的地方. 本贴只为了和大家交流学习,请勿用在其他地方,损害任何人的利益. 今天我,来说一下arp攻击的原理和教程 原理什么的还是自行百度好,因为专业的说明是严谨而又经得 ...
- Delphi7 流操作_压缩
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- python笔记(很乱)、打算抽个时间再好好整理
最近刚开始学python.总结的可能不是很好 print:打印值 input:可以进行等候赋值.进行一个交互 python中 需要两个==才为判断 变量:数字.字母.下划线组成 类型:int整数.st ...
- h5-携程页面小案例-伸缩盒子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue中在时间输入框中默认显示时间
<template> <card> <label>开始时间</label> <DatePicker v-model="startTime ...
- uWSGI调整buffer-size
https://uwsgi-docs.readthedocs.io/en/latest/Options.html#buffer-size buffer-size argument: required_ ...
- 前端Json 增加,删除,修改元素(包含json数组处理)
一:基础JSON对象 二:JSON数组数据 以下为增删修改方法: <!DOCTYPE html> <html lang="en"> <head> ...
- 201509-1 数列分段 Java
思路: 后一个和前一个不相等就算一段 import java.util.Scanner; public class Main { public static void main(String[] ar ...
- [Algo] 611. Compress String II
Given a string, replace adjacent, repeated characters with the character followed by the number of r ...