diff --git a/Solutions/S29.cpp b/Solutions/S29.cpp new file mode 100644 index 0000000..9c65b26 --- /dev/null +++ b/Solutions/S29.cpp @@ -0,0 +1,27 @@ +bool canFinish(int numCourses, vector>& prerequisites) { + list adj[numCourses]; + vector indeg(numCourses, 0), res; + queue q; + int n = prerequisites.size(); + for(int i = 0; i < n; i++) + { + int a = prerequisites[i][0], b = prerequisites[i][1]; + adj[b].push_back(a); + indeg[a]++; + } + for(int i = 0; i < numCourses; i++) + { + if(indeg[i] == 0) {q.push(i);} + } + while(!q.empty()) + { + int k = q.front(); + res.push_back(k); + q.pop(); + for(auto it = adj[k].begin(); it != adj[k].end(); it++) + { + if(--indeg[*it] == 0) q.push(*it); + } + } + return (res.size() == numCourses); +} \ No newline at end of file