-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseSchedule2.java
More file actions
55 lines (47 loc) · 1.83 KB
/
CourseSchedule2.java
File metadata and controls
55 lines (47 loc) · 1.83 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
/*
You are given an array prerequisites where prerequisites[i] = [a, b] indicates that you must take course b first if you want to take course a.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
There are a total of numCourses courses you are required to take, labeled from 0 to numCourses - 1.
Return a valid ordering of courses you can take to finish all courses. If there are many valid answers, return any of them. If it's not possible to finish all courses, return an empty array.
Example 1:
Input: numCourses = 3, prerequisites = [[1,0]]
Output: [0,1,2]
Explanation: We must ensure that course 0 is taken before course 1.
*/
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
List<Integer> adj_list[] = new ArrayList[numCourses];
int[] in_degree = new int[numCourses];
//init adjacency list
for(int i = 0;i < numCourses;i++){
adj_list[i] = new ArrayList<>();
}
// building graph and in-degree array
for(int[] pre : prerequisites){
int course = pre[0];
int req = pre[1];
adj_list[req].add(course);
in_degree[course]++;
}
Queue<Integer> q = new LinkedList<>();
int ind = 0;
for(int deg : in_degree){
if(deg == 0)
q.offer(ind);
ind++;
}
int res[] = new int[numCourses];
ind = 0;
// bfs from node having indegree 0
while(!q.isEmpty()){
int node = q.poll();
res[ind++] = node;
for(int neighbor : adj_list[node]){
in_degree[neighbor]--;
if(in_degree[neighbor] == 0)
q.offer(neighbor);
}
}
return ind == numCourses ? res : new int[0];
}
}