2 Star 10 Fork 2

国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_207.java 2.22 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
package com.hit.basmath.interview.top_interview_questions.hard_collection.trees_and_graphs;
import java.util.LinkedList;
import java.util.Queue;
/**
* 207. Course Schedule
* <p>
* There are a total of n courses you have to take, labeled from 0 to n-1.
* <p>
* 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]
* <p>
* Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
* <p>
* Example 1:
* <p>
* Input: 2, [[1,0]]
* Output: true
* <p>
* Explanation: There are a total of 2 courses to take.
* To take course 1 you should have finished course 0. So it is possible.
* <p>
* Example 2:
* <p>
* Input: 2, [[1,0],[0,1]]
* Output: false
* <p>
* 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.
* <p>
* Note:
* <p>
* 1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
* 2. You may assume that there are no duplicate edges in the input prerequisites.
*/
public class _207 {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[][] matrix = new int[numCourses][numCourses]; // i -> j
int[] indegree = new int[numCourses];
for (int[] prerequisite : prerequisites) {
int ready = prerequisite[0];
int pre = prerequisite[1];
if (matrix[pre][ready] == 0)
indegree[ready]++; //duplicate case
matrix[pre][ready] = 1;
}
int count = 0;
Queue<Integer> queue = new LinkedList();
for (int i = 0; i < indegree.length; i++) {
if (indegree[i] == 0) queue.offer(i);
}
while (!queue.isEmpty()) {
int course = queue.poll();
count++;
for (int i = 0; i < numCourses; i++) {
if (matrix[course][i] != 0) {
if (--indegree[i] == 0)
queue.offer(i);
}
}
}
return count == numCourses;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助