-
Notifications
You must be signed in to change notification settings - Fork 0
/
207. Course Schedule.cpp
39 lines (36 loc) · 1 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
/*
Problem Link: https://leetcode.com/problems/course-schedule
Time: 31 ms (Beats 63.24%), Space: 13.6 MB (Beats 73.66%)
*/
class Solution {
public:
bool canFinish(int V, vector<vector<int>>& prerequisites) {
vector<int> adj[V];
for(auto it: prerequisites){
adj[it[1]].push_back(it[0]);
}
int indegree[V];
for(int i=0;i<V;i++) indegree[i]=0;
for(int i=0;i<V;i++){
for(auto adjNode: adj[i]){
indegree[adjNode]++;
}
}
queue<int> q;
for(int i=0;i<V;i++){
if(indegree[i]==0) q.push(i);
}
vector<int> ans;
while(!q.empty()){
int node= q.front();
q.pop();
ans.push_back(node);
for(auto adjNode: adj[node]){
indegree[adjNode]--;
if(indegree[adjNode]==0) q.push(adjNode);
}
}
if(ans.size()==V) return true;
return false;
}
};