Topo_sort || python
Intuition\n Describe your first thoughts on how to solve this problem. \n1)concept is to find topo_sort for row and col...\n2)iterate from topo_sort of row elem
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1)concept is to find topo_sort for row and col...\n2)iterate from topo_sort of row elements and find its corresponding pos in topo_sort of col\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: Time complexity of topo sort\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> \n\n- Space complexity: O(k*k)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n \n\n# Code\n```\nclass Solution:\n    def buildMatrix(self, k: int, row: List[List[int]], col: List[List[int]]) -> List[List[int]]:\n        adj_l_A = [[] for i in range(k+1)]\n        indegree_A = [0]*(k+1)\n        for u,v in row:\n            adj_l_A[u].append(v)\n            indegree_A[v]+=1\n        queue1 = []\n        for i in range(1,k+1):\n            if indegree_A[i]==0:\n                queue1.append(i)\n        adj_l_B = [[] for i in range(k+1)]\n        indegree_B = [0]*(k+1)\n        for u ,v in col:\n            adj_l_B[u].append(v)\n            indegree_B[v]+=1\n        queue2 = []\n        for i in range(1,k+1):\n            if indegree_B[i]==0:\n                queue2.append(i)\n\n\n        \n        def topo_sort(queue,res,adj_list,indegree):\n            while queue:\n                curr = queue.pop(0)\n                res.append(curr)\n                for i in adj_list[curr]:\n                    indegree[i]-=1\n                    if indegree[i]==0:\n                        queue.append(i)\n            return res\n        Row = topo_sort(queue1,[],adj_l_A,indegree_A)\n        Col = topo_sort(queue2,[],adj_l_B,indegree_B)\n        if len(Row)==k and len(Col)==k:\n            grid = [[0 for i in range(k)] for j in range(k)]\n            ind = {}\n            for i in range(k):\n                ind[Col[i]] = i\n            for i in range(k):\n                j = ind[Row[i]]\n                grid[i][j] = Row[i]\n            return grid\n        return []\n\n\n        \n```
['Python3']