Kotlin. Beats 100% (623 ms). Extended "mapping" table + PriorityQueue
\n\n# Code\n\nclass Solution {\n    fun sortJumbled(mapping: IntArray, nums: IntArray): IntArray {\n        updateExtendedTable(mapping)\n\n        val pq = Pri
![image.png](https://assets.leetcode.com/users/images/6ead1c15-190b-4f46-b702-108c66d9f950_1721814281.9965765.png)\n\n# Code\n```\nclass Solution {\n    fun sortJumbled(mapping: IntArray, nums: IntArray): IntArray {\n        updateExtendedTable(mapping)\n\n        val pq = PriorityQueue<Number>()\n        var i = 0\n        while (i < nums.size) {\n            val n = nums[i]\n            pq.offer(Number(i, n, getMappedNumber(n)))\n            i++\n        }\n\n        i = 0\n        while (pq.isNotEmpty()) {\n            nums[i++] = pq.poll().value\n        }\n        return nums\n    }\n\n    fun updateExtendedTable(mapping: IntArray) {\n        for (i in 0..9) {\n            val eMapping = extendedMapping[i]\n            val b = base[i]\n            for (j in 0..9) {\n                eMapping[j] = b * mapping[j]\n            }\n        }\n    }\n\n    class Number(val index: Int, val value: Int, val mapped: Int): Comparable<Number> {\n        override fun compareTo(other: Number): Int {\n            val diff = mapped - other.mapped\n            if (diff != 0) return diff\n            return index - other.index\n        }\n    }\n\n    fun getMappedNumber(n: Int): Int {\n        if (n == 0) return extendedMapping[0][0]\n\n        var result = 0\n\n        var a = n\n        var i = 0\n        while (a > 0) {\n            result += extendedMapping[i][a % 10]\n            i++\n            a /= 10\n        }\n\n        return result\n    }\n\n    companion object {\n        val base = intArrayOf(\n            1, 10, 100,\n            1000, 10_000, 100_000,\n            1_000_000, 10_000_000, 100_000_000,\n            1_000_000_000\n        )\n        val extendedMapping = Array(10) { IntArray(10) }\n    }\n}\n```\n\n# Approach\n\nOne of my first versions of the solution used an inefficient number mapping algorithm:\n\n```\nfun getMappedNumber(n: Int, mapping: IntArray): Int {\n    if (n == 0) return mapping[0]\n\n    var result = 0\n    var base = 1\n    var a = n\n    while (a > 0) {\n        result += mapping[a % 10] * base\n        base *= 10\n        a /= 10\n    }\n    return result\n}\n```\n\nWhen I looked at the line with the multiplication on the "base"\n```\nresult += mapping[a % 10] * base\n```\nI wondered if it was possible to somehow bypass the multiplication operation in this place - after all, for the same digits and tens, we always get a mapping into the same digits and tens.\n\nThe solution was to calculate an extended version of the "mapping" table, in which the multiplication operation on the "base" was already included in the table itself. This gave a significant boost to the mapping algorithm:\n\n```\nfun getMappedNumber(n: Int): Int {\n    if (n == 0) return extendedMapping[0][0]\n\n    var result = 0\n\n    var a = n\n    var i = 0\n    while (a > 0) {\n        result += extendedMapping[i][a % 10]\n        i++\n        a /= 10\n    }\n\n    return result\n}\n```\n\nThe second optimization was the refusal to sort the calculated elements using an array and replacing it with sorting using a PriorityQueue - perhaps the algorithm began to work faster due to the reduction in the number of variables used.
['Kotlin']