-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCourse Schedule II.cpp
More file actions
37 lines (34 loc) · 981 Bytes
/
Copy pathCourse Schedule II.cpp
File metadata and controls
37 lines (34 loc) · 981 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
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> adj[numCourses];
for(auto& x : prerequisites){
adj[x[1]].push_back(x[0]);
}
vector<int>v3;
vector<int>indegree(numCourses, 0);
for (int i = 0; i < numCourses; i++) {
for (auto it : adj[i]) {
indegree[it]++;
}
}
queue<int> q;
for (int i = 0; i < numCourses; i++) {
if (indegree[i] == 0) {
q.push(i);
}
}
vector<int> topo;
while (!q.empty()) {
int node = q.front();
q.pop();
topo.push_back(node);
for (auto it : adj[node]) {
indegree[it]--;
if (indegree[it] == 0) q.push(it);
}
}
if(topo.size() == numCourses) return topo;
return {};
}
};