forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourseScheduleII.java
56 lines (54 loc) · 1.26 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
class Solution {
// TC : O(V+E)
// SC : O(V+E)
public int[] findOrder(int numCourses, int[][] prerequisites) {
int[] indegree = new int[numCourses];
// Course, List of Course Ids
Map<Integer, Set<Integer>> map = new HashMap<>();
for(int i=0;i<numCourses;i++){
map.put(i, new HashSet<>());
}
for(int[] prereq : prerequisites) {
int course = prereq[0];
int dependingCourse = prereq[1];
// Egde from dependingCourse towards the current course
map.get(dependingCourse).add(course);
indegree[course]++;
}
// Course Ids
Queue<Integer> qu = new LinkedList<>();
for(int i=0;i<numCourses;i++) {
if(indegree[i] == 0){
qu.offer(i);
}
}
List<Integer> order = new ArrayList<>();
while(!qu.isEmpty()){
int size = qu.size();
while(size-->0){
int head = qu.poll();
if(indegree[head]==0){
//indegree[head]--;
order.add(head);
}
for(Integer children : map.get(head)){
indegree[children]-- ;
if(indegree[children] == 0){
qu.offer(children);
}
}
}
}
int i = 0;
int[] ans = new int[order.size()];
for(Integer el: order){
ans[i] = el;
i++;
}
if(order.size() != numCourses){
// few pending courses and those cant be completed
return new int[]{};
}
return ans;
}
}