Runtime 99ms || C++ || TopoSorting Algorithm || Time complexity O(K+E) & Space complexity O(k^2)
Complexity\n- Time complexity:O(K+E)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(k^2)  //for final matrix\n Add your space complexity he
# Complexity\n- Time complexity:O(K+E)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(k^2)  //for final matrix\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    void topoSortUtil(stack<int>& stk, vector<int>& vis, int curr, vector<vector<int>>& adjMat, bool& cycle){\n        if(cycle) return;\n\n        vis[curr] = 1;\n\n        for(int i=0; i<adjMat[curr].size(); i++){\n            int neighbour = adjMat[curr][i];\n            if(vis[neighbour]==0){\n                topoSortUtil(stk, vis, neighbour, adjMat, cycle);\n            }else if(vis[neighbour]==1) {\n                cycle = true;\n                return;\n            }\n        }\n        \n        vis[curr] = 2;\n        stk.push(curr);\n    }\n\n    vector<int> topoSort(vector<vector<int>>& conditions, int k){\n        vector<vector<int>> adjMat(k);\n\n        stack<int> stk;\n        vector<int> vis(k, 0);\n        vector<int> order;\n\n        //matrix adjacency\n        for(int i=0; i<conditions.size(); i++){  \n            int src = conditions[i][0];\n            int dest = conditions[i][1];\n            adjMat[src-1].push_back(dest-1);\n        }\n        \n        bool cycle = false;\n        for(int i=0; i<k;i++){\n            if(vis[i]==0){\n                topoSortUtil(stk, vis, i, adjMat, cycle);  \n                if(cycle==true) return {};             \n            }\n        }\n\n        while(!stk.empty()){\n            order.push_back(stk.top()+1);\n            stk.pop();\n        }\n\n        return order;\n        \n    }\n\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n       \n\n        vector<int> orderForRowCond = topoSort(rowConditions, k); // for topoSort Algo\n        vector<int> orderForColCond = topoSort(colConditions, k);\n\n\n        if(orderForRowCond.size()==0 || orderForColCond.size()==0) return {};  //check for cycle\n        \n\n        vector<vector<int>> finalMat(k, vector<int> (k, 0));  //initalizing final matrix\n\n        unordered_map<int,int> colIdx;   //storing positions of numbers from 1-k\n\n        for(int i=0; i<orderForColCond.size(); i++){\n            colIdx[orderForColCond[i]] = i;\n        }\n\n        for(int i=0; i<orderForRowCond.size(); i++){  //making final matrix\n            int idx = colIdx[orderForRowCond[i]];\n            finalMat[i][idx] = orderForRowCond[i];\n\n        }\n\n\n        return finalMat;\n    }\n};\n```
['C++']