-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path210.h
38 lines (34 loc) · 1.01 KB
/
210.h
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
//
// Created by 17336 on 2022/5/24.
//
#ifndef HOT100_210_H
#define HOT100_210_H
#include <basi.h>
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>> &prerequisites) {
vector<vector<int>> edges(numCourses);
vector<int> pres(numCourses);
for (const auto &prerequisite: prerequisites) {
++pres[prerequisite[0]];
edges[prerequisite[1]].push_back(prerequisite[0]);
}
vector<int> preZeros;
for (int i = 0; i < numCourses; ++i) {
if (pres[i] == 0) preZeros.push_back(i);
}
vector<int> res;
while (!preZeros.empty()) {
int item=preZeros.back();
preZeros.pop_back();
res.push_back(item);
for (const auto &item1: edges[item]) {
--pres[item1];
if (pres[item1] == 0) preZeros.push_back(item1);
}
}
if(res.size()!=numCourses) return {};
return res;
}
};
#endif //HOT100_210_H