forked from armankhondker/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
courseScheduleII.java
56 lines (44 loc) · 2.22 KB
/
courseScheduleII.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// There are a total of n courses you have to take, labeled from 0 to n-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 prerequisite pairs, 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.
//Intuition, can represent the nodes as graph,
//Naive solution, If there is any cycle we can't finish the courses
//Can use topological ordering to see if
//To make topigical ordering
//1. list all elements with incoming edges
//2. list an element to where all incoming edges to that element have already been listeed
//USE BFS to sort the graph
//TC:
//SC:
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
if (numCourses == 0) return null;
// Convert graph presentation from edges to indegree of adjacent list.
int indegree[] = new int[numCourses], order[] = new int[numCourses], index = 0;
for (int i = 0; i < prerequisites.length; i++) // Indegree - how many prerequisites are needed.
indegree[prerequisites[i][0]]++;
Queue<Integer> queue = new LinkedList<Integer>();
for (int i = 0; i < numCourses; i++)
if (indegree[i] == 0) {
// Add the course to the order because it has no prerequisites.
order[index++] = i;
queue.offer(i);
}
// How many courses don't need prerequisites.
while (!queue.isEmpty()) {
int prerequisite = queue.poll(); // Already finished this prerequisite course.
for (int i = 0; i < prerequisites.length; i++) {
if (prerequisites[i][1] == prerequisite) {
indegree[prerequisites[i][0]]--;
if (indegree[prerequisites[i][0]] == 0) {
// If indegree is zero, then add the course to the order.
order[index++] = prerequisites[i][0];
queue.offer(prerequisites[i][0]);
}
}
}
}
return (index == numCourses) ? order : new int[0];
}
}