-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy path207.java
More file actions
38 lines (33 loc) · 1.06 KB
/
207.java
File metadata and controls
38 lines (33 loc) · 1.06 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
class Solution {
public boolean CyclicChecker(List<Integer>[] adj, int[] visited, int vertex){
if(visited[vertex] == 1){
return false;
}else if(visited[vertex] == 2){
return true;
}
visited[vertex] = 1;
for(int x : adj[vertex]){
if(CyclicChecker(adj, visited ,x) == false){
return false;
}
}
visited[vertex] = 2;
return true;
}
public boolean canFinish(int numCourses, int[][] prerequisites) {
List<Integer>[] l = new ArrayList[numCourses];
for(int i=0;i<l.length;i++){
l[i] = new ArrayList<>();
}
for(int[] elem : prerequisites){
l[elem[0]].add(elem[1]);
}
int[] visited = new int[numCourses];
for(int i = 0; i< numCourses; ++i){
if((visited[i] == 0) && (CyclicChecker(l, visited, i) == false)){
return false;
}
}
return true;
}
}