-
Notifications
You must be signed in to change notification settings - Fork 0
/
58_course-schedule.cpp
61 lines (50 loc) · 1.4 KB
/
58_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
49
50
51
52
53
54
55
56
57
58
59
60
61
// cpp-blind-75/58_course-schedule.cpp
/**
* Date : 09-Aug-2023
* Repo: https://github.com/ankitsamaddar/
*
* Problem : Course Schedule
* Difficulty: 🟡Medium
*
* Leetcode 0207 : https://leetcode.com/problems/course-schedule
* Lintcode 0615 : https://www.lintcode.com/problem/615
*/
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
// initialize empty list for each course
for (int i = 0; i < numCourses; i++) {
preMap[i] = {};
}
// hash the prerequisites to the map
for (vector<int>& pre : prerequisites) {
preMap[pre[0]] .push_back(pre[1]);
}
// check if cycle is detected for each course
for (int i = 0; i < numCourses; i++) {
if (!dfs(i)) return false;
}
return true;
}
private:
unordered_map<int, vector<int>> preMap;
unordered_set<int> visitedSet; // keep track of visited node in DFS traversal
bool dfs(int crs) {
// if already visited
if (visitedSet.count(crs)) {
return false;
}
// add course to list
visitedSet.insert(crs);
// for each pre-requisite course of the current course
// do DFS traversal
for (int pre : preMap[crs]) {
if (!dfs(pre)) return false;
}
// Reset current course as it is already checked
visitedSet.erase(crs);
preMap[crs] = {};
// true as current course has no cycles
return true;
}
};