-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseScheduler2.cpp
More file actions
38 lines (38 loc) · 1.2 KB
/
CourseScheduler2.cpp
File metadata and controls
38 lines (38 loc) · 1.2 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
//using indegree to make it classical!!
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
vector<int> res;
vector<unordered_set<int>> graph = getGraph(numCourses,prerequisites);
vector<int> indegree = get_indegree(numCourses,graph);
for (int i = 0 ; i < numCourses;i++){
int j = 0;
for (;j < numCourses;j++){
if (!indegree[j])break;
}
if (j == numCourses) return vector<int>();
indegree[j]=-1;
res.push_back(j);
for (auto t:graph[j]){
indegree[t]--;
}
}
return res;
}
vector<unordered_set<int>> getGraph(int numCourses, vector<pair<int,int>>& prerequisites){
vector<unordered_set<int>> vu(numCourses);
for (auto x: prerequisites){
vu[x.second].insert(x.first);
}
return vu;
}
vector<int> get_indegree(int numCourses, vector<unordered_set<int>>& vu){
vector<int> indegree(numCourses,0);
for (auto x:vu){
for (auto t:x){
indegree[t]++;
}
}
return indegree;
}
};