Created
May 23, 2022 01:41
-
-
Save HarshKumarChoudary/bb868631c85392b3d00ba07bd06a2bb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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