-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourse Schedule.java
38 lines (30 loc) · 948 Bytes
/
Course Schedule.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
enum State { kInit, kVisiting, kVisited }
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
List<Integer>[] graph = new List[numCourses];
State[] states = new State[numCourses];
for (int i = 0; i < numCourses; ++i)
graph[i] = new ArrayList<>();
for (int[] prerequisite : prerequisites) {
final int u = prerequisite[1];
final int v = prerequisite[0];
graph[u].add(v);
}
for (int i = 0; i < numCourses; ++i)
if (hasCycle(graph, i, states))
return false;
return true;
}
private boolean hasCycle(List<Integer>[] graph, int u, State[] states) {
if (states[u] == State.kVisiting)
return true;
if (states[u] == State.kVisited)
return false;
states[u] = State.kVisiting;
for (final int v : graph[u])
if (hasCycle(graph, v, states))
return true;
states[u] = State.kVisited;
return false;
}
}