-
Notifications
You must be signed in to change notification settings - Fork 0
/
Course Schedule.cpp
40 lines (33 loc) · 1.28 KB
/
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
/*
Please note the SelectNode function is not the optimal one. It takes O(n) time. The efficient approach will be to maintain a queue for BFS (Stack for DFS).Add
any vertices to this data sturcture when its degree first becomes zero.
*/
class Solution {
public:
bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> CoursesRequire(numCourses,0);
map<int, vector<int>> NodeEdges;
vector<int> EdgesElement;
int node;
for(int i = 0; i<prerequisites.size();i++){
CoursesRequire[prerequisites[i].first]++;
NodeEdges[prerequisites[i].second].push_back(prerequisites[i].first);
}
for(int i = 0; i < numCourses; i++){
node = SelectNode(CoursesRequire);
if (node == -1)
return false;
for (int j = 0 ;j < NodeEdges[node].size();j++)
CoursesRequire[NodeEdges[node][j]]--;
CoursesRequire[node] = -2;
}
return true;
}
// 0 means no prerequisite, -2 means visited
int SelectNode(vector<int> &CoursesRequire){
for (int i = 0 ; i < CoursesRequire.size();i++)
if (CoursesRequire[i] == 0)
return i;
return -1;
}
};