-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseScheduler.cpp
More file actions
38 lines (37 loc) · 1.26 KB
/
CourseScheduler.cpp
File metadata and controls
38 lines (37 loc) · 1.26 KB
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
// The intuitive way but a more common way si to detect indegee of the graph nodes
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
if (!prerequisites.size()) return true;
vector<bool> alreadyDone(numCourses,true);
for(auto x : prerequisites) alreadyDone[x.first] = false;
unordered_map<int,vector<int>> um;
buildGraph(um,prerequisites);
while(xtend(alreadyDone,um));
for (auto x: alreadyDone){
if (x == false) return false;
}
return true;
}
void buildGraph(unordered_map<int,vector<int>>& um,vector<pair<int, int>>& prerequisites){
for (auto x:prerequisites){
um[x.first].push_back(x.second);
}
}
bool canlearn(pair<int,vector<int>> t,vector<bool>& alreadyDone){
for (auto x : t.second){
if (!alreadyDone[x]) return false;
}
return true;
}
bool xtend(vector<bool>& alreadyDone, unordered_map<int,vector<int>>& um){
bool cont = false;
for (auto x : um){
if (!alreadyDone[x.first] && canlearn(x,alreadyDone)){
alreadyDone[x.first] = true;
cont = true;
}
}
return cont;
}
};