Beats 100% || Easiest and Cleanest Logic || Topological Sort
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> getIndegree(vector<int> adj[], int k)\n    {\n        vector<int> indegree(k, 0);\n\n        for (int i = 0; i < k; i++)\n        {\n            for (auto u : adj[i])\n                indegree[u]++;\n        }\n\n        return indegree;\n    }\n\n    vector<int> topologicalSort(vector<vector<int>>& conditions, int k)\n    {\n        vector<int> adj[k];\n\n        for (int i = 0; i < conditions.size(); i++)\n            adj[conditions[i][0] - 1].push_back(conditions[i][1] - 1);\n\n        vector<int> indegree = getIndegree(adj, k);\n        queue<int> q;\n        vector<int> order;\n\n        for (int i = 0; i < k; i++)\n        {\n            if (indegree[i] == 0)\n                q.push(i);\n        }\n\n        while (!q.empty())\n        {\n            int u = q.front();\n            order.push_back(u);\n            q.pop();\n\n            for (auto v : adj[u])\n            {\n                indegree[v]--;\n                if (indegree[v] == 0)\n                    q.push(v);\n            }\n        }\n\n        if (order.size() != k)\n            return {};\n\n        return order;\n    }\n\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowc, vector<vector<int>>& colc) {\n\n        vector<int> horizontalOrder = topologicalSort(rowc, k);\n        vector<int> verticalOrder = topologicalSort(colc, k);\n\n        if (horizontalOrder.empty() || verticalOrder.empty()) \n            return {};\n\n        vector<vector<int>> matrix(k, vector<int>(k, 0));\n\n        unordered_map<int, int> rowPos, colPos;\n\n        for (int i = 0; i < k; i++) {\n            rowPos[horizontalOrder[i]] = i;\n            colPos[verticalOrder[i]] = i;\n        }\n\n        for (int i = 0; i < k; i++) {\n            matrix[rowPos[i]][colPos[i]] = i + 1;\n        }\n\n        return matrix;\n    }\n};\n\n```
['C++']