-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_25.java
More file actions
32 lines (30 loc) · 857 Bytes
/
Copy pathDay_25.java
File metadata and controls
32 lines (30 loc) · 857 Bytes
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
// Course Schedule II
class Solution {
public int[] findOrder(int numc, int[][] pr) {
int[] topo = new int[numc];
int[] indeg = new int[numc];
List<List<Integer>> adj = new ArrayList<>();
for(int i=0;i<numc;i++){
adj.add(new ArrayList<>());
}
for(var it : pr){
adj.get(it[1]).add(it[0]);
indeg[it[0]]++;
}
Queue<Integer> q = new LinkedList<>();
for(int i=0;i<numc;i++){
if(indeg[i] == 0) q.offer(i);
}
int i = 0;
while(!q.isEmpty()){
int node = q.poll();
topo[i++] = node;
for(var it : adj.get(node)){
indeg[it]--;
if(indeg[it] == 0) q.offer(it);
}
}
if(i<numc) return new int[]{};
return topo;
}
}