forked from strang3-r/Leetcode75
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseSchedular.cpp
More file actions
59 lines (35 loc) · 1 KB
/
CourseSchedular.cpp
File metadata and controls
59 lines (35 loc) · 1 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
56
57
58
59
class Solution {
public:
vector<int> findOrder(int V, vector<vector<int>>& a) {
vector<int>adj[V+1];
for(int i=0;i<a.size();i++){
adj[a[i][1]].push_back(a[i][0]);
}
vector<int>indegree(V,0);
for(int i=0;i<V;i++){
for(auto it : adj[i]){
indegree[it]++;
}
}
queue<int>q;
for(int i=0;i<V;i++){
if(indegree[i]==0){
q.push(i);
}
}
vector<int>v;
while(!q.empty()){
int x=q.front();
v.push_back(x);
q.pop();
for(auto it : adj[x]){
indegree[it]--;
if(indegree[it]==0)q.push(it);
}
}
if(v.size()!=V){
return {};
}
return v;
}
};