-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCourse Schedule GFG
35 lines (34 loc) · 925 Bytes
/
Course Schedule GFG
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
class Solution
{
public:
vector<int> findOrder(int n, int m, vector<vector<int>> prerequisites)
{
//created adj
vector<int> adj[n];
for(auto it: prerequisites){
adj[it[1]].push_back(it[0]);
}
//calculated the degree of nodes
int inDegree[n] = {0};
for(int i=0;i<n;i++){
for(auto it: adj[i]) inDegree[it]++;
}
//push degree in queue having degree 0
queue<int> q;
for(int i=0;i<n;i++){
if(inDegree[i] == 0) q.push(i);
}
vector<int> topo;
while(!q.empty()){
int node = q.front();
q.pop();
topo.push_back(node);
for(auto it: adj[node]){
inDegree[it]--;
if(inDegree[it] == 0) q.push(it);
}
}
if(topo.size() == n) return topo;
return {};
}
};