Kotlin. Beats 100% (476 ms). IntArrays to sort row and col positions.
\n\n# Code\n\nclass Solution {\n    fun buildMatrix(k: Int, rowConditions: Array<IntArray>, colConditions: Array<IntArray>): Array<IntArray> {\n        val rows
![image.png](https://assets.leetcode.com/users/images/b17bf514-4417-4bfc-af38-c07a0fd3acb8_1721630927.6569366.png)\n\n# Code\n```\nclass Solution {\n    fun buildMatrix(k: Int, rowConditions: Array<IntArray>, colConditions: Array<IntArray>): Array<IntArray> {\n        val rows = IntArray(k + 1)\n        for (i in 0 until k) {\n            rows[i + 1] = i\n        }\n        if (sort(k, rowConditions, rows)) {\n            return EMPTY\n        }\n\n        val cols = IntArray(k + 1)\n        for (i in 0 until k) {\n            cols[i + 1] = i\n        }\n        if (sort(k, colConditions, cols)) {\n            return EMPTY\n        }\n\n        val matrix = Array(k) { IntArray(k) }\n        for (i in 1..k) {\n            matrix[rows[i]][cols[i]] = i\n        }\n        return matrix\n    }\n\n    private fun sort(k: Int, conditions: Array<IntArray>, positions: IntArray): Boolean {\n        var swapped = false\n        for (i in 0 until k) {\n            swapped = false\n            for ((low, hi) in conditions) {\n                val lowPos = positions[low]\n                val hiPos = positions[hi]\n                if (lowPos > hiPos) {\n                    swapped = true\n                    positions[low] = hiPos\n                    positions[hi] = lowPos\n                }\n            }\n            if (!swapped) break\n        }\n        return swapped\n    }\n\n    companion object {\n        val EMPTY = Array(0) { IntArray(0) }\n    }\n}\n```
['Kotlin']