-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathCourseSchedule.java
71 lines (64 loc) · 2.09 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package problems.medium;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Created by sherxon on 1/25/17.
*/
public class CourseSchedule {
public static void main(String[] args) {
System.out.println(canFinish(6, new int[][]{
{0, 1},
{0, 4},
{0, 3},
{4, 1},
{4, 2},
{1, 3},
{1, 5},
{1, 2},
{3, 5},
{2, 5},
{2, 0},
}));
}
static boolean canFinish(int numCourses, int[][] prerequisites) {
if (numCourses < 2) return true;
List<Set<Integer>> sets = new ArrayList<>();
for (int i = 0; i < numCourses; i++)
sets.add(new HashSet<>());
int[] inDegrees = new int[numCourses];
for (int[] edge : prerequisites) {
sets.get(edge[0]).add(edge[1]);
inDegrees[edge[1]]++;
}
boolean[] read = new boolean[numCourses];
for (int head = 0; head < numCourses; head++) {
if (inDegrees[head] == 0) {
boolean[] recStack = new boolean[numCourses];
boolean[] visited = new boolean[numCourses];
boolean hasCycle = topSort(sets, head, visited, recStack);
for (int i = 0; i < visited.length; i++) {
if (visited[i]) read[i] = true;
}
if (hasCycle) return false;
}
}
for (int i = 0; i < read.length; i++) {
if (!read[i]) return false;
}
return true;
}
private static boolean topSort(List<Set<Integer>> sets, Integer head, boolean[] visited, boolean[] recStack) {
visited[head] = true;
recStack[head] = true;
for (Integer child : sets.get(head)) {
if (!visited[child] && topSort(sets, child, visited, recStack))
return true;
else if (recStack[child])
return true;
}
recStack[head] = false;
return false;
}
}