-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourse Schedule II.cpp
33 lines (32 loc) · 993 Bytes
/
Course Schedule II.cpp
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
class Solution
{
public:
vector<int> findOrder(int n, vector<vector<int>>& pr)
{
map<int,vector<int>>mp;
for(int i=0 ; i<pr.size() ; i++)
mp[pr[i][1]].push_back(pr[i][0]);
vector<int>indegree(n,0);
for(auto x:mp)
for(auto y:x.second)
indegree[y]++;
queue<int>q;
for(int i=0 ; i<n ; i++)
if(indegree[i]==0)
q.push(i);
vector<int>res;
while(!q.empty())
{
int node = q.front();
q.pop();
res.push_back(node);
for(auto x:mp[node])
{
indegree[x]--;
if(indegree[x]==0) q.push(x);
}
}
if(res.size()!=n) return {};
return res;
}
};