标签:leetcode
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?
For example:
2, [[1,0]]
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
2, [[1,0],[0,1]]
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.
这道题目是典型的图的拓扑排序题目,这里先简要介绍一下拓扑排序算法的实现步骤:
所以,这道题目就是检测最后构造的图中是否有环即可(即检测indegree数组是否有顶点的值大于0)。
ac代码如下所示:
public class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
boolean canFinish = true;
int[] indegree = new int[numCourses];
int[][] matrix = new int[numCourses][numCourses];
// initial data
for (int i = 0; i < prerequisites.length; i ++) {
if (matrix[prerequisites[i][0]][prerequisites[i][1]] == 1) continue;
indegree[prerequisites[i][1]] ++;
matrix[prerequisites[i][0]][prerequisites[i][1]] = 1;
}
// topological sort
for (int i = 0; i < numCourses; i ++) {
for (int j = 0; j < numCourses; j ++) {
if (indegree[j] == 0) {
indegree[j] --;
// delete node which start is j
for (int k = 0; k < numCourses; k ++) {
if (matrix[j][k] == 1) {
indegree[k] --;
matrix[j][k] = 0;
}
}
break;
}
}
}
// output result
for (int i = 0; i < numCourses; i ++) {
if (indegree[i] > 0) {
canFinish = false;
break;
}
}
return canFinish;
}
}
[LeetCode]Course Schedule,解题报告
标签:leetcode
原文地址:http://blog.csdn.net/wzy_1988/article/details/45644693