C++ - Topological Sort | beats 100% in both Time and Space Complexity
By using topo sort we are generating the order of sequence of numbers in row,column and here we also check whether there are any cycles in row,col directed grap
By using topo sort we are generating the order of sequence of numbers in row,column and here we also check **whether there are any cycles in row,col directed graph**.\nIf there is any cycle in row or col directed graph, then we return an empty array\nWe are using topo sort because there is a **relation** between numbers. **eg: [1,2] means 1 must occur before 2, so there will be directed edge from 1 -> 2.**\n```\nbool findOrder(vector<vector<int>>& a, int k, unordered_map<int, int> &m){\n        vector<int> adj[k+1];\n        vector<int> indegree(k+1, 0);\n        queue<int> q;\n        int num = 0;\n        \n        for(int i=0;i< a.size();i++){\n            adj[a[i][0]].push_back(a[i][1]);\n            indegree[a[i][1]]++;\n        }\n        \n        for(int i=1;i< k+1;i++){\n            if(indegree[i] == 0) q.push(i);\n        }\n        \n        if(!q.size()) return false;\n        \n        while(!q.empty()){\n            int val = q.front();\n            q.pop();\n            indegree[val]--;\n            \n            m[val] = num;\n            num++;\n            \n            for(auto i : adj[val]){\n                indegree[i]--;\n                if(indegree[i] == 0) q.push(i);\n            }\n        }\n        \n        return (m.size() != k) ? false : true;\n    }\n    \n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n        vector<vector<int>> ans(k, vector<int>(k, 0));\n        vector<vector<int>> temp;\n        \n        unordered_map<int, int> row, col;\n        \n        bool a = findOrder(rowConditions,k,row);\n        bool b = findOrder(colConditions,k,col);\n        \n        if(!a | !b) return temp;\n        \n        for(int i=1;i<= k;i++){\n            ans[row[i]][col[i]] = i;\n        }\n        return ans;\n    }
['Topological Sort', 'C']