-
Notifications
You must be signed in to change notification settings - Fork 1
/
Course Schedule
48 lines (39 loc) · 1.4 KB
/
Course Schedule
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
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
for(int i=0;i<numCourses;i++){
graph.add(new ArrayList<>());
}
for(int i=0;i<prerequisites.length;i++){
int u=prerequisites[i][0];
int v=prerequisites[i][1];
graph.get(v).add(u);
}
//detect a cycle dfs
boolean[] recStack = new boolean[numCourses];
boolean[] visited = new boolean[numCourses];
for(int i=0;i<graph.size();i++){
if(visited[i]==false){
if(dfs(i,graph,visited,recStack)){
return false;
}
}
}
return true;
}
private boolean dfs(int currentVertex,ArrayList<ArrayList<Integer>> graph, boolean[] visited, boolean[] recStack){
if(visited[currentVertex]==false){
visited[currentVertex]=true;
recStack[currentVertex]=true;
for(int neighbour:graph.get(currentVertex)){
if(visited[neighbour]==false&&dfs(neighbour,graph,visited,recStack)){
return true;
} else if(recStack[neighbour]==true){
return true;
}
}
recStack[currentVertex]=false;
}
return false;
}
}