210 Course Schedule II

210. Course Schedule II

1. Question

There are a total ofncourses you have to take, labeled from0ton - 1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:[0,1]
Given the total number of courses and a list of prerequisitepairs, return the ordering of courses you should take to finish all courses.
There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
For example:
1
2, [[1,0]]
Copied!
There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is[0,1]
1
4, [[1,0],[2,0],[3,1],[3,2]]
Copied!
There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is[0,1,2,3]. Another correct ordering is[0,2,1,3].
Note:
  1. 1.
    The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about.
  2. 2.
    You may assume that there are no duplicate edges in the input prerequisites.

2. Implementation

(1) BFS
1
class Solution {
2
public int[] findOrder(int numCourses, int[][] prerequisites) {
3
int[] res = new int[numCourses];
4
5
List<Set<Integer>> adjList = new ArrayList<>();
6
7
for (int i = 0; i < numCourses; i++) {
8
adjList.add(new HashSet<>());
9
}
10
11
int[] inDegree = new int[numCourses];
12
13
for (int[] prerequisite : prerequisites) {
14
adjList.get(prerequisite[1]).add(prerequisite[0]);
15
++inDegree[prerequisite[0]];
16
}
17
18
Queue<Integer> queue = new LinkedList<>();
19
20
for (int i = 0; i < numCourses; i++) {
21
if (inDegree[i] == 0) {
22
queue.add(i);
23
}
24
}
25
26
int order = 0;
27
while (!queue.isEmpty()) {
28
int course = queue.remove();
29
res[order++] = course;
30
31
for (int nextCourse : adjList.get(course)) {
32
if (--inDegree[nextCourse] == 0) {
33
queue.add(nextCourse);
34
}
35
}
36
}
37
return order == numCourses ? res : new int[0];
38
}
39
}
Copied!
(2) DFS
1
class Solution {
2
public int[] findOrder(int numCourses, int[][] prerequisites) {
3
int n = numCourses;
4
List<Set<Integer>> adjList = new ArrayList<>();
5
6
for (int i = 0; i < n; i++) {
7
adjList.add(new HashSet<>());
8
}
9
10
for (int[] prerequisite : prerequisites) {
11
adjList.get(prerequisite[1]).add(prerequisite[0]);
12
}
13
14
boolean[] visited = new boolean[n];
15
boolean[] onStack = new boolean[n];
16
17
Stack<Integer> stack = new Stack<>();
18
19
for (int i = 0; i < n; i++) {
20
if (!visited[i]) {
21
if (!dfs(i, visited, onStack, adjList, stack)) {
22
return new int[0];
23
}
24
}
25
}
26
27
int index = 0;
28
int[] res = new int[n];
29
30
while (!stack.isEmpty()) {
31
res[index++] = stack.pop();
32
}
33
return res;
34
}
35
36
public boolean dfs(int course, boolean[] visited, boolean[] onStack, List<Set<Integer>> adjList, Stack<Integer> stack) {
37
if (visited[course]) {
38
return true;
39
}
40
41
if (onStack[course]) {
42
return false;
43
}
44
45
onStack[course] = true;
46
47
for (int nextCourse : adjList.get(course)) {
48
if (!dfs(nextCourse, visited, onStack, adjList, stack)) {
49
return false;
50
}
51
}
52
53
onStack[course] = false;
54
visited[course] = true;
55
stack.push(course);
56
return true;
57
}
58
}
Copied!

3. Time & Space Complexity

BFS: 时间复杂度O(n), 空间复杂度O(n)
DFS: 时间复杂度O(n), 空间复杂度O(n)