Skip to content

2191. Sort the Jumbled Numbers

Intuition

The task requires us to sort an array of integers based on a custom mapping of their digits. This can be approached by first transforming each number using the provided mapping and then sorting the transformed numbers while maintaining the original order of elements with identical transformed values.

Approach: Transform and Sort

We will create a helper function to convert each number in the array based on the given mapping. Then, we'll pair the transformed numbers with their original indices to maintain stability while sorting. Finally, we sort these pairs and construct the result based on the sorted order of the transformed values.

Explanation:

  1. Helper Function convert:
  2. If x is 0, return mapping[0].
  3. Initialize res to store the result and expo to track the place value (units, tens, etc.).
  4. Iterate through each digit of x:
  5. Replace the digit with its mapped value.
  6. Accumulate the transformed value in res.
  7. Update the place value (expo) by multiplying it by 10.
  8. Return the transformed value res.
  9. Main Function sortJumbled:
  10. Initialize res to store the sorted result and pairs to store the pairs of transformed values and their original indices.
  11. Convert each number in nums using the convert function and store the pair of the converted value and its index in pairs.
  12. Sort pairs based on the transformed values.
  13. Transform the sorted pairs back to the original values in res using their stored indices.

Complexity

  • Time complexity: O(n log n), where n is the length of nums.
  • Space complexity: O(n)

Code

class Solution {
public:
    int convert(int x, vector<int>& mapping) {
        if (x == 0) return mapping[0];

        int res = 0, expo = 1;
        while (x) {
            res += mapping[x % 10] * expo;
            expo *= 10;
            x /= 10;
        }

        return res;
    }

    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
        int n = nums.size();

        vector<int> res(n);
        vector<pair<int, int>> pairs(n);

        for (int i = 0; i < n; i++) {
            pairs[i] = make_pair(convert(nums[i], mapping), i);
        }

        ranges::sort(pairs);
        ranges::transform(pairs, res.begin(), [&](auto& p) {
            return nums[p.second];
        });

        return res;
    }
};