-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanFinish.java
More file actions
46 lines (44 loc) · 1.38 KB
/
canFinish.java
File metadata and controls
46 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Author : WindAsMe
* File : canFinish.java
* Time : Create on 18-9-4
* Location : ../Home/JavaForLeeCode2/canFinish.java
* Function : LeetCode No.207
*/
public class canFinish {
// Foremost is to find the Graph has the circle.
private static boolean canFinishResult(int numCourses, int[][] prerequisites) {
// sustain the array
int[] inDegree = new int[numCourses];
// add all the input to array
for (int[] i : prerequisites)
inDegree[i[1]]++;
while (true) {
int index = -1;
for (int i = 0; i < inDegree.length; i++) {
if (inDegree[i] == 0) {
// Mark this index
inDegree[i] = -1;
index = i;
break;
}
}
if (index != -1) {
for (int[] prerequisite : prerequisites) {
if (prerequisite[0] == index)
inDegree[prerequisite[1]]--;
}
} else {
for (int i : inDegree) {
if (i != -1)
return false;
}
return true;
}
}
}
public static void main(String[] args) {
int[][] pre = {{0, 1}, {1, 0}};
System.out.println(canFinishResult(2, pre));
}
}