-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path207.course-schedule.java
43 lines (41 loc) · 1.12 KB
/
207.course-schedule.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
39
40
41
42
/*
* @lc app=leetcode id=207 lang=java
*
* [207] Course Schedule
*/
// @lc code=start
class Solution {
/*
topological sort
time: O(V+E)
space: O(V+E)
*/
public boolean canFinish(int numCourses, int[][] prerequisites) {
List[] graph = new List[numCourses];
for (int i = 0; i < numCourses; ++i) {
graph[i] = new ArrayList<>(); // cannot use Arrays.fill()
}
int[] indegree = new int[numCourses];
for (int[] pair : prerequisites) {
graph[pair[0]].add(pair[1]);
indegree[pair[1]]++;
}
Deque<Integer> queue = new LinkedList<>();
for (int i = 0; i < numCourses; ++i) {
if (indegree[i] == 0) queue.offer(i);
}
while (!queue.isEmpty()) {
int cur = queue.poll();
numCourses--;
List<Integer> list = graph[cur];
for (int next : list) {
indegree[next]--;
if (indegree[next] == 0) {
queue.offer(next);
}
}
}
return numCourses == 0;
}
}
// @lc code=end