Skip to content
LC-2191 Medium LeetCode

2191. Sort the Jumbled Numbers

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 60% Topics: Array, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogm + nlogn), m is the max of nums
# Space: O(n)

# sort
class Solution(object):
    def sortJumbled(self, mapping, nums):
        """
        :type mapping: List[int]
        :type nums: List[int]
        :rtype: List[int]
        """
        def transform(mapping, x):
            if not x:
                return mapping[x]
            result, base = 0, 1
            while x:
                result += mapping[x%10]*base
                x //= 10
                base *= 10
            return result

        return [nums[i] for _, i in sorted((transform(mapping, nums[i]), i) for i in xrange(len(nums)))]

Solution from kamyu104/LeetCode-Solutions · MIT

Similar questions