-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0207-course-schedule.cpp
48 lines (44 loc) · 1.39 KB
/
0207-course-schedule.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
/*
Courses & prerequisites, return true if can finish all courses
Ex. numCourses = 2, prerequisites = [[1,0]] -> true
All courses can be completed if there's no cycle (visit already visited)
Time: O(V + E)
Space: O(V + E)
*/
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
// map each course to prereq list
unordered_map<int, vector<int>> m;
for (int i = 0; i < prerequisites.size(); i++) {
m[prerequisites[i][0]].push_back(prerequisites[i][1]);
}
// all courses along current DFS path
unordered_set<int> visited;
for (int course = 0; course < numCourses; course++) {
if (!dfs(course, m, visited)) {
return false;
}
}
return true;
}
private:
bool dfs(int course, unordered_map<int, vector<int>>& m, unordered_set<int>& visited) {
if (visited.find(course) != visited.end()) {
return false;
}
if (m[course].empty()) {
return true;
}
visited.insert(course);
for (int i = 0; i < m[course].size(); i++) {
int nextCourse = m[course][i];
if (!dfs(nextCourse, m, visited)) {
return false;
}
}
m[course].clear();
visited.erase(course);
return true;
}
};