Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HarshKumarChoudary/bb868631c85392b3d00ba07bd06a2bb3 to your computer and use it in GitHub Desktop.
Save HarshKumarChoudary/bb868631c85392b3d00ba07bd06a2bb3 to your computer and use it in GitHub Desktop.
class Solution
{
public:
vector<int> findOrder(int n, int m, vector<vector<int>> pre)
{
vector<int>ans;
vector<int>indeg(n,0);
vector<int>adj[n];
queue<int>q;
for(auto a:pre){
adj[a[1]].push_back(a[0]);
indeg[a[0]]++;
}
for(int i = 0; i < n; ++ i){
if(indeg[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int cur = q.front();
q.pop();
ans.push_back(cur);
for(auto a:adj[cur]){
indeg[a]--;
if(indeg[a] == 0){
q.push(a);
}
}
}
if(ans.size () == n){
return ans;
}
return {};
//code here
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment