forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CourseSchedule.java
34 lines (32 loc) · 1.02 KB
/
CourseSchedule.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
import java.util.*;
public class CourseSchedule {
/**
* 这题就是典型的拓扑排序
*/
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] indegrees = new int[numCourses];
HashMap<Integer, Set<Integer>> map = new HashMap<>();
for (int[] pos : prerequisites) {
Set<Integer> set = map.getOrDefault(pos[1], new HashSet<>());
map.put(pos[1], set);
if (set.add(pos[0])) {
indegrees[pos[0]]++;
}
}
Queue<Integer> queue = new LinkedList<>();
for (int i = 0; i < indegrees.length; i++) {
if (indegrees[i] == 0) {
queue.offer(i);
}
}
for ( ; !queue.isEmpty(); numCourses--) {
int n = queue.poll();
for (Integer k : map.getOrDefault(n, new HashSet<>())) {
if (--indegrees[k] == 0) {
queue.offer(k);
}
}
}
return numCourses == 0;
}
}