forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CourseScheduleII.java
38 lines (36 loc) · 1.11 KB
/
CourseScheduleII.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
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class CourseScheduleII {
public int[] findOrder(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
Set<Integer>[] sets = new HashSet[numCourses];
for (int i = 0; i < sets.length; i++) {
sets[i] = new HashSet<>();
}
for (int[] pre : prerequisites) {
if (sets[pre[1]].add(pre[0])) {
indegree[pre[0]]++;
}
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < numCourses; i++) {
if (indegree[i] == 0) {
queue.add(i);
}
}
int count = 0;
int[] result = new int[numCourses];
while (!queue.isEmpty()) {
Integer n = queue.poll();
result[count++] = n;
for (Integer k : sets[n]) {
if (--indegree[k] == 0) {
queue.add(k);
}
}
}
return count == numCourses ? result : new int[0];
}
}