Simple toposort question
\n\n# Complexity\n- Time complexity:O(n^2)// n^2 can be the maximum number of edges\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add
\n\n# Complexity\n- Time complexity:O(n^2)// n^2 can be the maximum number of edges\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n   vector<int> solve(vector<vector<int>> adj){\n    int n=adj.size();\n    vector<int> indegree(n,0);\n    for(auto i:adj){\n        for(auto j:i){\n            indegree[j]++;\n        }\n\n    }\n\n    queue<int> q;\n\n    for(auto i=0;i<n;i++){\n        if(indegree[i]==0)\n        q.push(i);\n    }\n\n    vector<int> ans;\n\n    while(!q.empty()){\n        int x=q.front();\n        ans.push_back(x);\n        q.pop();\n        for(auto i:adj[x]){\n            indegree[i]--;\n            if(indegree[i]==0)\n            q.push(i);\n        }\n    }\n\n    if(ans.size()<n)\n    return {};\n    return ans;\n   }\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rc, vector<vector<int>>& cc) {\n        vector<vector<int>> ans(k,vector<int> (k,0));\n\n        vector<vector<int>> adj1(k),adj2(k);\n\n        for(auto i:rc){\n            adj1[--i[0]].push_back(--i[1]);\n        }\n        for(auto i:cc){\n            adj2[--i[0]].push_back(--i[1]);\n        }\n\n        auto v1=solve(adj1),v2=solve(adj2);\n\n        if(v1.empty() || v2.empty())\n        return {};\n        unordered_map<int,int> row,col;\n        for(int i=0;i<k;i++){\n            row[++v1[i]]=i;\n            col[++v2[i]]=i;\n        }\n\n        for(int i=1;i<=k;i++){\n            ans[row[i]][col[i]]=i;\n        }\n\n        return ans;\n\n\n        \n\n        \n    }\n};\n```
['Array', 'Graph', 'Topological Sort', 'Matrix', 'C++']