代码拉取完成,页面将自动刷新
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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。