-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseScheduleII.java
More file actions
58 lines (54 loc) · 1.44 KB
/
Copy pathCourseScheduleII.java
File metadata and controls
58 lines (54 loc) · 1.44 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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[] res = new int[numCourses];
int[] inDegree = new int[numCourses];
for(int i = 0; i < prerequisites.length; i++){
inDegree[prerequisites[i][1]]++;
}
Queue<Integer> queue = new LinkedList<>();
Set<Integer> map = new HashSet<>();
boolean flag = false;
for(int i = 0; i < numCourses; i++){
if(inDegree[i] == 0){
queue.offer(i);
map.add(i);
flag = true;
}
}
if(!flag) return new int[]{};
int count = 0;
while(!queue.isEmpty()){
int size = queue.size();
for(int i = 0; i < size; i++){
int node = queue.poll();
count++;
res[numCourses - count] = node;
for(int j = 0; j < prerequisites.length; j++){
if(prerequisites[j][0] == node){
inDegree[prerequisites[j][1]]--;
}
}
for(int j = 0; j < numCourses; j++){
if(!map.contains(j) && inDegree[j] == 0){
queue.offer(j);
map.add(j);
}
}
}
}
for(int j = 0; j < numCourses; j++)
if(inDegree[j] != 0) return new int[]{};
return res;
}
public static void main(String[] args) {
CourseScheduleII course = new CourseScheduleII();
int[] res = course.findOrder(2, new int[][]{});
//int[] res = course.findOrder(2, new int[][]{{0,1},{1,0}});
for(int num : res)
System.out.print(num + " ");
}
}