[Python3] topological sort
Please pull this commit for solutions of weekly 308. \n\nIntuition\nThe workhorse of this problem is topological sort. The orders in aboveConditions and leftCon
Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/ea6bc01b091cbf032b9c7ac2d3c09cb4f5cd0d2d) for solutions of weekly 308. \n\n**Intuition**\nThe workhorse of this problem is topological sort. The orders in `aboveConditions` and `leftConditions` construct two graphs. Via topological sort, the order becomes apparent. \nSo the algo is summarized below:\n1) topological sort the two graphs to get the row orders and column orders; \n2) if no cycle is found in either row order or column order, an answer exists; otherwise, return empty matrix; \n3) for each number from 1 to k, find its row number and column number from the results of the sorting; \n4) fill the numbers into the matrix. \n\n**Analysis**\nTime complexity `O(K^2)`\nSpace complexity `O(K^2)`\n\n```\nclass Solution:\n    def buildMatrix(self, k: int, rowConditions: List[List[int]], colConditions: List[List[int]]) -> List[List[int]]:\n        \n        def fn(cond): \n            """Return topological sort"""\n            graph = [[] for _ in range(k)]\n            indeg = [0]*k\n            for u, v in cond: \n                graph[u-1].append(v-1)\n                indeg[v-1] += 1\n            queue = deque(u for u, x in enumerate(indeg) if x == 0)\n            ans = []\n            while queue: \n                u = queue.popleft()\n                ans.append(u+1)\n                for v in graph[u]: \n                    indeg[v] -= 1\n                    if indeg[v] == 0: queue.append(v)\n            return ans \n        \n        row = fn(rowConditions)\n        col = fn(colConditions)\n        \n        if len(row) < k or len(col) < k: return []\n        ans = [[0]*k for _ in range(k)]\n        row = {x : i for i, x in enumerate(row)}\n        col = {x : j for j, x in enumerate(col)}\n        for x in range(1, k+1): ans[row[x]][col[x]] = x\n        return ans \n```
['Python3']