-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210_findOrder.cpp
More file actions
33 lines (33 loc) · 1.01 KB
/
210_findOrder.cpp
File metadata and controls
33 lines (33 loc) · 1.01 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
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> res;
vector<int> empty;
int indegree[numCourses];
vector<vector<int>> graph(numCourses);
queue<int> q;
for(int i = 0;i<numCourses;i++)
indegree[i] = 0;
for(int i = 0;i<prerequisites.size();i++){
int pre = prerequisites[i][1],p = prerequisites[i][0];
indegree[p]++;
graph[pre].push_back(p);
}
for(int i = 0;i<numCourses;i++)
if(indegree[i] == 0)
q.push(i);
int courses = 0;
while(!q.empty()){
courses++;
int c = q.front();
q.pop();
res.push_back(c);
for(int i = 0;i<graph[c].size();i++){
indegree[graph[c][i]]--;
if(indegree[graph[c][i]] == 0)
q.push(graph[c][i]);
}
}
return courses==numCourses?res:empty;
}
};