-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path207.course-schedule.cpp
42 lines (39 loc) · 1.17 KB
/
207.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
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
map<int, vector<int>> outDegree;
vector<int> inDegree(numCourses, 0);
for(auto course: prerequisites){
inDegree[course[0]] += 1;
if(course[0] == course[1])
return false;
outDegree[course[1]].push_back(course[0]);
}
// cout<<"vector : ";
// for(auto i: inDegree){
// cout<<i<<"\t";
// }
// cout<<endl;
for(int i=0; i<numCourses; i++){
int index = -1;
// cout<<"vector : ";
// for(auto i: inDegree){
// cout<<i<<"\t";
// }
// cout<<endl;
for(auto i = 0; i<inDegree.size(); i++){
if(inDegree[i] == 0){
index = i;
break;
}
}
// cout<<index<<endl;
if(index == -1)
return false;
inDegree[index] = -1;
for(auto itr : outDegree[index])
inDegree[itr]--;
}
return true;
}
};