-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path210. Course Schedule II.cpp
37 lines (28 loc) · 1.05 KB
/
210. 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
34
35
36
37
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector <int> indegree (numCourses, 0);
vector <int> * l = new vector <int> [numCourses];
queue <int> pendingNodes;
vector <int> ans;
for( auto edge : prerequisites){
indegree[edge[0]]++;
l[edge[1]].push_back(edge[0]);
}
for(int i = 0 ; i < numCourses ; i ++){
if(indegree[i] == 0)
pendingNodes.push(i);
}
while(!pendingNodes.empty()){
int current = pendingNodes.front();
pendingNodes.pop();
ans.push_back(current);
for(auto nbr : l[current]){
indegree[nbr]--;
if(indegree[nbr] == 0)
pendingNodes.push(nbr);
}
}
return ( ans.size() != numCourses ? vector <int> () : ans);
}
};