207. Course Schedule
There are a total of n courses you have to take labelled from 0 to n - 1.
Some courses may have prerequisites, for example, if prerequisites[i] = [ai, bi] this means you must take the course bi before the course ai.
Given the total number of courses numCourses and a list of the prerequisite pairs, return the ordering of courses you should take to finish all courses.
If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.
1
2
3
4
5
| Example 1:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: [0,1]
Explanation: 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
2
3
4
5
6
| Example 2:
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
Explanation: 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].
|
1
2
3
4
| Example 3:
Input: numCourses = 1, prerequisites = []
Output: [0]
|
Constraints:
- 1 <= numCourses <= 2000
- 0 <= prerequisites.length <= numCourses * (numCourses - 1)
- prerequisites[i].length == 2
- 0 <= ai, bi < numCourses
- ai != bi
- All the pairs [ai, bi] are distinct.
Jamboard#




Solution#
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
59
60
61
62
63
64
65
66
67
| class Solution {
int[] q = new int[5_000];
int head = 0;
int tail = 0;
public boolean canFinish(int n, int[][] prerequisites) {
boolean[] visited = new boolean[n];
List<List<Integer>> G = new ArrayList<>();
int[] indegree = new int[n];
for (int i = 0; i < n; i++) {
G.add(new ArrayList<>());
}
for (int i = 0; i < prerequisites.length; i++) {
int[] course = prerequisites[i];
G.get(course[1]).add(course[0]);
indegree[course[0]]++;
}
for (int i = 0; i < n; i++) {
if (indegree[i] == 0) q[tail++] = i;
}
int count = 0;
while (head != tail) {
int v = q[head++];
for (Integer e: G.get(v)) {
indegree[e]--;
if (indegree[e] == 0) q[tail++] = e;
}
count++;
}
return count == n;
}
// 0/1/2 unprocessed, processing, processed
int[] visited;
int[][] g;
public boolean canFinish2(int n, int[][] prerequisites) {
visited = new int[n];
g = prerequisites;
for (int i = 0; i < n; i++) {
if (visited[i] == 0) {
boolean cycle = dfs(i);
if (cycle) return false;
}
}
return true;
}
boolean dfs(int s) {
visited[s] = 1;
for (int i = 0; i < g.length; i++) {
int[] course = g[i];
if (course[1] == s) {
int v = course[0];
if (visited[v] == 0) {
boolean cycle = dfs(v);
if (cycle) return true;
} else if (visited[v] != 2) {
return true;
}
}
}
visited[s] = 2;
return false;
}
}
|
Solution - Kosaraju Algorithm#
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
59
60
61
62
63
64
65
66
| class Solution {
int[] visited;
int[] who;
Stack<Integer> st = new Stack<>();
List<Integer>[] G;
List<Integer>[] revG;
int count = 0;
public boolean canFinish(int numCourses, int[][] prerequisites) {
visited = new int[numCourses];
who = new int[numCourses];
G = new List[numCourses];
revG = new List[numCourses];
for (int i = 0; i < numCourses; i++) {
G[i] = new ArrayList<>();
revG[i] = new ArrayList<>();
}
for (int[] pr: prerequisites) {
int before = pr[0];
int after = pr[1];
G[before].add(after);
revG[after].add(before);
}
for (int i = 0; i < numCourses; i++) {
if (visited[i] == 0) {
dfs(i);
}
}
Arrays.fill(visited, 0);
while (st.size() > 0) {
int n = st.pop();
if (visited[n] == 0) {
count++;
dfs1(n, n);
}
}
System.out.println(Arrays.toString(who));
return numCourses == count;
}
void dfs(int i) {
visited[i] = 1;
for (int u: G[i]) {
if (visited[u] == 0) {
dfs(u);
}
}
st.push(i);
}
void dfs1(int i, int rep) {
visited[i] = 1;
who[i] = rep;
for (int u: revG[i]) {
if (visited[u] == 0) {
dfs1(u, rep);
}
}
}
}
|