-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourse Schedule.cpp
More file actions
48 lines (41 loc) · 921 Bytes
/
Course Schedule.cpp
File metadata and controls
48 lines (41 loc) · 921 Bytes
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
// Topo sort
// BFS
class Solution
{
public:
vector<int> findOrder(int n, int m, vector<vector<int>> prerequisites)
{
vector<int> adj[n];
vector<int> indegree(n,0);
for(auto it:prerequisites)
{
adj[it[1]].push_back(it[0]);
indegree[it[0]]++;
}
queue<int> q;
for(int i=0;i<n;i++)
{
if(indegree[i]==0)
q.push(i);
}
vector<int> ans;
while(!q.empty())
{
int f=q.front();
q.pop();
ans.push_back(f);
for(auto it:adj[f])
{
indegree[it]--;
if(indegree[it]==0)
q.push(it);
}
}
for(auto it:indegree)
{
if(it>=1)
return {};
}
return ans;
}
};