Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
- vector<int>id(numCourses),ans;
- map<int,vector<int>>mp;
- for(auto v:prerequisites){
- id[v[0]]++;
- mp[v[1]].push_back(v[0]);
- }
- queue<int>q;
- for(int i=0;i<id.size();i++){
- if(id[i]==0)q.push(i);
- }
- int cnt=0;
- while(!q.empty()){
- int x=q.front();
- ans.push_back(x);
- q.pop();
- for(auto v:mp[x]){
- id[v]--;
- if(id[v]==0)q.push(v);
- }
- cnt++;
- }
- if(cnt!=numCourses)return {};
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement