Skip to main content
Back to LeetCode Archive
Jul 24, 20245 min read

2191. Sort the Jumbled Numbers (24 July 2024)

ArraySortingMap

Problem Description

Given a mapping array mapping of size 10 and an array of integers nums, your task is to sort the nums array based on the values obtained by remapping each digit of each number according to mapping.

You can find the problem statement on LeetCode.

Solution Explanation

To solve this problem, we need to sort the nums array based on the values obtained by applying a custom digit mapping to each number. Here's a step-by-step explanation of how we can achieve this:

1. Use a Vector to Store Mapped Values

  • We use a vector of pairs to store each number along with its mapped value.
  • The mapped value is obtained by replacing each digit of the number with its corresponding value from the mapping array.

2. Calculate Mapped Values

  • Iterate through the nums array and compute the mapped value for each number.
  • Store each number and its mapped value as a pair in the vector.

3. Sort the Vector

  • Sort the vector of pairs based on the mapped values.

4. Extract Sorted Results

  • Extract the original numbers from the sorted vector of pairs.

Here's the C++ code implementing this solution:

class Solution {
public:
    vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        
        vector<pair<int, int>> mapped;
        mapped.reserve(nums.size()); // Reserve space to avoid multiple allocations
 
        for (auto num : nums) {
            int n = num;
            int val = 0;
            if (n == 0) {
                val = mapping[0];
            } else {
                int factor = 1;
                while (n > 0) {
                    val += mapping[n % 10] * factor;
                    n /= 10;
                    factor *= 10;
                }
            }
            mapped.emplace_back(num, val);
        }
 
        sort(mapped.begin(), mapped.end(),
             [](const pair<int, int>& p1, const pair<int, int>& p2) {
                 return p1.second < p2.second;
             });
 
        vector<int> result;
        result.reserve(nums.size()); // Reserve space to avoid multiple allocations
        for (const auto& elem : mapped) {
            result.push_back(elem.first);
        }
 
        return result;
    }
};

Mapping and Sorting Process

  • Vector of Pairs (mapped):
    • Used to store the original number and its mapped value.
    • Reserving space for the vector to avoid multiple allocations improves performance.

Example

Consider the following inputs:

  • mapping = [8, 9, 4, 0, 2, 1, 3, 5, 7, 6]
  • nums = [990, 332, 981]

Step-by-Step Execution

  1. Calculate the mapped values for each number:

    • For 990: Mapped value = 662
    • For 332: Mapped value = 440
    • For 981: Mapped value = 671
  2. Store each number and its mapped value in the vector:

    • mapped after calculation:
    { {990, 662}, {332, 440}, {981, 671} }
  3. Sort the vector based on mapped values:

    • Resulting order:
    { {332, 440}, {990, 662}, {981, 671} }
  4. Extract the original numbers in sorted order:

    • Resulting order:
    [332, 990, 981]

Conclusion

The described method uses a vector of pairs to efficiently map each number to its custom value and sort based on these values. This approach leverages the sorting capability of the C++ sort function with a custom comparator to achieve the desired result. This solution is efficient and easy to understand, making it a practical choice for solving the problem of sorting numbers based on a custom digit mapping.