✅C++|| Kahn's Algo || Topological Sort || Beginner friendly
\nclass Solution {\npublic:\n    vector<int> kahn(vector<vector<int>>conditions, int k)\n    {\n        vector<int>graph[k+1];\n        vector<int>degree(k+1,0)
```\nclass Solution {\npublic:\n    vector<int> kahn(vector<vector<int>>conditions, int k)\n    {\n        vector<int>graph[k+1];\n        vector<int>degree(k+1,0);\n        vector<int>ans;\n        queue<int>q;\n        for(vector<int>condition : conditions)\n        {\n            graph[condition[0]].push_back(condition[1]);\n            degree[condition[1]]++;\n        }\n        for(int i=1;i<k;i++) { if(degree[i]==0) q.push(i); }\n        while(!q.empty())\n        {\n            int node = q.front(); \n            ans.push_back(node); \n            q.pop();\n            for(int neighbour : graph[node]) \n            {   \n                degree[neighbour]--;   \n                if(degree[neighbour]==0)\n                {\n                    q.push(neighbour);\n                }\n            }\n        }\n        \n        return ans;       \n    }\n    vector<vector<int>> buildMatrix(int k, vector<vector<int>>& rowConditions, vector<vector<int>>& colConditions) {\n        vector<int>row_order = kahn(rowConditions, k+1); \n        vector<int>col_order = kahn(colConditions, k+1); \n        \n        unordered_map<int,int>m;\n        if(row_order.size()<k || col_order.size() <k)return  {};\n        vector<vector<int>>ans(k,vector<int>(k,0));\n        \n        for(int i=0;i<k;i++)\n        {\n            m[col_order[i]] = i;\n        }\n        \n        for(int i=0;i<k;i++)\n        {\n            int column_position = m[row_order[i]]; \n            ans[i][column_position] = row_order[i];\n        }\n        return ans;\n    }\n};\n```\n\n**Please upvote if it was helpful for you, thank you!**
['Topological Sort', 'C']