Kotlin Topological sort and build matrix
Using topo sort Kann algorithm on row and column separately and then use that to rebuild matrix. Since only k number from 1..k in k * k matrix, each number can 
Using topo sort Kann algorithm on row and column separately and then use that to rebuild matrix. Since only k number from 1..k in k * k matrix, each number can be on one row and one column, no overlap\n```\nclass Solution {\n    fun topologicalSort(edges: Array<IntArray>, n: Int): List<Int> {\n        val graph = mutableMapOf<Int, MutableList<Int>>()\n        val inDegree = IntArray(n + 1)\n        val order = mutableListOf<Int>()\n        for ((before, after) in edges) {\n            graph[before] = graph.getOrDefault(before, mutableListOf<Int>())\n            graph[before]!!.add(after)\n            inDegree[after] += 1\n        }\n        val toVisit = ArrayDeque<Int>((1..n).filter({ inDegree[it] == 0 }))\n        while (toVisit.isNotEmpty()) {\n            val node = toVisit.removeFirst()\n            order.add(node)\n            for (nextNode in graph.getOrDefault(node, emptyList())) {\n                inDegree[nextNode] -= 1\n                if (inDegree[nextNode] == 0) {\n                    toVisit.add(nextNode)\n                }\n            }  \n        }\n        if (order.size != n) {\n            return emptyList()\n        }\n        return order\n    }\n    \n    fun buildMatrix(k: Int, rowConditions: Array<IntArray>, colConditions: Array<IntArray>): Array<IntArray> {\n        val rowOrder = topologicalSort(rowConditions, k)\n        val colOrder = topologicalSort(colConditions, k)\n        if (rowOrder.size == 0 || colOrder.size == 0) {\n            return emptyArray()\n        }\n        val matrix = Array(k) { IntArray(k) }\n        val colMap = colOrder.mapIndexed { index, num -> num to index }.toMap()\n        for ((row, num) in rowOrder.withIndex()) {\n            matrix[row][colMap[num]!!] = num\n        }\n        return matrix\n    }\n}\n```
['Topological Sort', 'Kotlin']