forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0210-course-schedule-ii.cpp
56 lines (51 loc) · 1.77 KB
/
0210-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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Courses & prerequisites, return ordering of courses to take to finish all courses
Ex. numCourses = 2, prerequisites = [[1,0]] -> [0,1], take course 0 then 1
All courses can be completed if there's no cycle, check for cycles
Time: O(V + E)
Space: O(V + E)
*/
class Solution {
public:
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
unordered_map<int, vector<int>> m;
// build adjacency list of prereqs
for (int i = 0; i < prerequisites.size(); i++) {
m[prerequisites[i][0]].push_back(prerequisites[i][1]);
}
unordered_set<int> visit;
unordered_set<int> cycle;
vector<int> result;
for (int course = 0; course < numCourses; course++) {
if (!dfs(course, m, visit, cycle, result)) {
return {};
}
}
return result;
}
private:
// a course has 3 possible states:
// visited -> course added to result
// visiting -> course not added to result, but added to cycle
// unvisited -> course not added to result or cycle
bool dfs(int course, unordered_map<int, vector<int>>& m, unordered_set<int>& visit,
unordered_set<int>& cycle, vector<int>& result) {
if (cycle.find(course) != cycle.end()) {
return false;
}
if (visit.find(course) != visit.end()) {
return true;
}
cycle.insert(course);
for (int i = 0; i < m[course].size(); i++) {
int nextCourse = m[course][i];
if (!dfs(nextCourse, m, visit, cycle, result)) {
return false;
}
}
cycle.erase(course);
visit.insert(course);
result.push_back(course);
return true;
}
};