-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path207-Course-Schedule.cpp
More file actions
38 lines (33 loc) · 906 Bytes
/
Copy path207-Course-Schedule.cpp
File metadata and controls
38 lines (33 loc) · 906 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
35
36
37
38
class Solution {
public:
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
int n=numCourses;
vector<vector<int>>adj(n);
for(auto&it:prerequisites){
int x=it[0];
int y=it[1];
adj[y].push_back(x);
}
vector<int>indegree(n,0);
for(int i=0;i<n;i++){
for(int j=0;j<adj[i].size();j++){
indegree[adj[i][j]]++;
}
}
queue<int>q;
for(int i=0;i<indegree.size();i++){
if(indegree[i]==0)q.push(i);
}
vector<int>ans;
while(!q.empty()){
int curr=q.front();
q.pop();
ans.push_back(curr);
for(auto&it:adj[curr]){
indegree[it]--;
if(indegree[it]==0)q.push(it);
}
}
return (ans.size()==n);
}
};