Topological Sort, Easy C++ Solution.
Intuition\n Describe your first thoughts on how to solve this problem. \nSince the problem is related to which row or column appears before the other.. we need 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince the problem is related to which row or column appears before the other.. ***we need to get the order of which element comes before which element***. Only **topological sort** gives such order. So first get the order of the rows and columns then place them in the matrix.\n\n**Note:-** If you didnt get the idea of topological sort... It is similar to the question : https://leetcode.com/problems/course-schedule-ii/description/ so first you can try out this then it will be very easy to understand and solve the current problem. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Use ***topological sort*** and get the ordering or the row and column elements. \n2. Then from row ordering you will get the row index and from the column ordering you will get the column index, so in this way you can fill the matrix.\n3. ***KeyPoint to note is that roworder and columnorder will have the same elements, so you can find rowindex and columnindex by searching for a specific element in both the orderings.***\n \nNote:- If the topological sort didnt work properly (cycle is present) in either rowconditions or colconditions then return an empty 2D array. \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(k^2 + E)\nWhere E is the number of edges in the conditions.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(k^2 + E)\nWhere E is the number of edges in the conditions.\n\n# Code\n```\nclass Solution {\npublic:\n\n    void order(vector<int>& ans,vector<vector<int>>& conditions,int k){\n        vector<vector<int>>adj(k+1);\n        for(int i = 0;i<conditions.size();i++) {\n            adj[conditions[i][0]].push_back(conditions[i][1]);\n        }\n        vector<int>indegree(k+1,0);\n        for(int i = 0;i<=k;i++) {\n            for(auto j : adj[i]) {\n                indegree[j]++;\n            }\n        }\n        queue<int>q;\n        for(int i =1;i<=k;i++) {\n            if(indegree[i]==0) {\n                q.push(i);\n            }\n        }\n        while(!q.empty()){\n            int x = q.front();\n            q.pop();\n            ans.push_back(x);\n            for(auto neighbour : adj[x]){\n                indegree[neighbour]--;\n                if(indegree[neighbour]==0) {\n                    q.push(neighbour);\n                }\n            }\n        }\n        if(ans.size()!=k){\n            ans.clear();\n        } \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>>invalid;\n        vector<int>roworder;\n        order(roworder,rowConditions,k);\n        vector<int>columnorder;\n        order(columnorder,colConditions,k);\n        if(roworder.empty() || columnorder.empty()){\n            return invalid;\n        }\n        \n        for(int i = 1;i<=k;i++){\n            int row;\n            int col;\n            for(int j = 0;j<roworder.size();j++){\n                if(roworder[j]==i){\n                    row = j;\n                    break;\n                }\n            }\n            for(int j =0;j<columnorder.size();j++){\n                if(columnorder[j]==i){\n                    col = j;\n                    break;\n                }\n            }\n            ans[row][col] = i;\n        }\n        return ans;\n    }\n};\n```
['Topological Sort', 'C++']