-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207_Course_Schedule.c++
More file actions
34 lines (33 loc) · 892 Bytes
/
207_Course_Schedule.c++
File metadata and controls
34 lines (33 loc) · 892 Bytes
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
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
vector<vector<int>>adj(numCourses);
for(auto it: prerequisites){
adj[it[1]].push_back(it[0]);
}
vector<int>indegree(numCourses,0);
for(int i=0; i<numCourses; i++){
for(auto nbr:adj[i]){
indegree[nbr]++;
}
}
queue<int>q;
for(int i=0; i<numCourses; i++){
if(indegree[i]==0){
q.push(i);
}
}
int count=0;
while(!q.empty()){
int node = q.front();
q.pop();
count++;
for(auto nbr: adj[node]){
indegree[nbr]--;
if(indegree[nbr]==0)q.push(nbr);
}
}
if(count==numCourses)return true;
return false;
}
};