-
Notifications
You must be signed in to change notification settings - Fork 5
/
CourseScheduleII.java
82 lines (72 loc) · 2.71 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Problem Statement:
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.
Problem Link:
Course Schedule 2: https://leetcode.com/problems/course-schedule-ii/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/CourseScheduleII.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
int[] list;
int pointer = 0;
public int[] findOrder(int numCourses, int[][] prerequisites) {
list = new int[numCourses];
if(!topologicalSortUsingKahnsAlgortihm(numCourses, prerequisites))
return new int[0];
return list;
}
public boolean topologicalSortUsingKahnsAlgortihm(int numCourses, int[][] prerequisites){
boolean[][] adj = new boolean[numCourses][numCourses];
for(int i=0;i<prerequisites.length;i++)
adj[prerequisites[i][1]][prerequisites[i][0]] = true;
int degree[] = new int[numCourses];
Queue<Integer> queue = new ArrayDeque<>();
int visited =0 ;
setupDegrees(adj, degree, queue);
if(queue.isEmpty())
return false;
while(!queue.isEmpty()){
int node = (int) queue.remove();
visited++;
list[pointer++] = node;
decreaseDegreeOfNeighbors(adj, node, degree, queue);
}
if(visited==numCourses)
return true;
return false;
}
public void setupDegrees(boolean adj[][], int[] degree, Queue queue){
for(int i=0;i<adj.length;i++){
degree[i]=getDegree(adj, i);
if(degree[i]==0){
queue.add(i);
}
}
}
public int getDegree(boolean adj[][], int node){
int degree =0;
for(int i =0;i<adj.length;i++){
if(adj[i][node] == true)
degree++;
}
return degree;
}
public void decreaseDegreeOfNeighbors(boolean adj[][], int node, int degree[], Queue queue){
for(int i=0;i<adj.length;i++){
if(adj[node][i]==true){
degree[i]--;
if(degree[i]==0){
queue.add(i);
}
}
}
}
}