-
Notifications
You must be signed in to change notification settings - Fork 0
/
courseSchedule.java
42 lines (33 loc) · 1.01 KB
/
courseSchedule.java
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
public boolean canFinish(int numCourses, int[][] lists) {
if (lists == null || lists.length == 0) {
return true;
}
int from = 0, to = 0;
boolean[] edges = new boolean[lists.length];
boolean found = true;
int[] count = new int[numCourses];
for (int i = 0; i < lists.length; i++) {
int key = lists[i][1];
count[key]++;
}
while (found) {
found = false;
for (int i = 0; i < lists.length; i++) {
if (!edges[i]) {
from = lists[i][0];
to = lists[i][1];
if (count[from] == 0 && count[to] != 0) {
edges[i] = true;
found = true;
count[to]--;
}
}
}
}
for (int i : count) {
if (i > 0) {
return false;
}
}
return true;
}