Skip to content

Commit

Permalink
Solution to #29
Browse files Browse the repository at this point in the history
  • Loading branch information
bharatr21 committed May 30, 2020
1 parent c86a7df commit 8dd2752
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Solutions/S29.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
list<int> adj[numCourses];
vector<int> indeg(numCourses, 0), res;
queue<int> 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);
}

0 comments on commit 8dd2752

Please sign in to comment.