-
Notifications
You must be signed in to change notification settings - Fork 9
/
CourseScheduleII.cpp
39 lines (36 loc) · 1.22 KB
/
CourseScheduleII.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
38
39
// Problem: https://leetcode.com/problems/course-schedule-ii/
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
class CourseScheduler {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> outgoing;
vector<vector<int>> incoming;
vector<int> result;
unordered_set<int> seen;
outgoing.resize(numCourses, 0);
incoming.resize(numCourses, {});
for (int i = 0; i < prerequisites.size(); ++i) {
outgoing[prerequisites[i][0]]++;
incoming[prerequisites[i][1]].push_back(prerequisites[i][0]);
}
while (true) {
bool flag = false;
for (int i = 0; i < numCourses; ++i) {
if (outgoing[i] == 0 && seen.find(i) == seen.end()) {
flag = true;
seen.insert(i);
result.push_back(i);
for (int j = 0; j < incoming[i].size(); ++j) {
outgoing[incoming[i][j]]--;
}
}
}
if (not flag) break;
}
if (result.size() != numCourses) result.clear();
return result;
}
};