-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path207.Coarse Schedule
40 lines (34 loc) · 1.03 KB
/
207.Coarse 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
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int visited[]=new int[numCourses];
ArrayList<ArrayList<Integer>> adj=new ArrayList<>();
for(int i=0;i<numCourses;i++){
adj.add(new ArrayList<>());
}
int indegree[]=new int[numCourses];
for(int i=0;i<prerequisites.length;i++){
adj.get(prerequisites[i][1]).add(prerequisites[i][0]);
indegree[prerequisites[i][0]]++;
}
System.out.print(Arrays.toString(indegree));
Queue<Integer>qu=new LinkedList<>();int c=0;
for(int i=0;i<numCourses;i++){
if(indegree[i]==0){
System.out.print(i);
qu.add(i);
c++;
}
}
while(!qu.isEmpty()){
int a=qu.poll();
for(int i:adj.get(a)){
indegree[i]--;
if(indegree[i]==0){
qu.add(i);
c++;
}
}
}
return c==adj.size();
}
}