Published 2024-07-23

2191. Sort the Jumbled Numbers

leetcode

Submission

Problem:

You are given an integer array `mapping` which ranges from 0 to 9 and an array of integers `nums`. The `mapping` representation of an integer is the new integer obtained by replacing each occurrence of digit `i` in the integer with `mapping[i]` for all 0 <= i <= 9.

For example, if `mapping = [2,1,4,8,6,3,0,9,7,5]` and `nums = [990, 332, 981]`, then:

  • The mapped value of `990` is `550`.
  • The mapped value of `332` is `883`.
  • The mapped value of `981` is `583`.

Your task is to return a sorted array of `nums` such that `nums[i]` is less than or equal to `nums[j]` if their mapped values are in non-decreasing order.

Solution Description:

To implement this solution, we need to:

  1. Convert each number in `nums` to its mapped value using the `mapping` array.
  2. Pair each original number with its mapped value.
  3. Sort these pairs based on the mapped values.
  4. Extract the original numbers from the sorted pairs.

Time Complexity: O(n log n), dominated by the sorting step. Space Complexity: O(n), for storing the pairs and the intermediate mapped values.

Example:

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

  • Mapped values are: `550` (for `990`), `883` (for `332`), `583` (for `981`).
  • After sorting by mapped values: `[990, 981, 332]`.

Setup:

Create a general framework for this solution, including the skeleton for the function and a setup for testing using `console.log`.

Test Execution:

Design tests that verify input parameters, actual results, expected results, and whether the tests passed or failed, ensuring complete coverage.

Always Provide testcases Example:

```js


/**
 * @param {number[]} mapping - An array representing the digit mapping.
 * @param {number[]} nums - An array of integers to be sorted based on the mapping values.
 * @return {number[]} The sorted array of numbers based on their mapped values.
 */
function sortJumbledNumbers(mapping, nums) {
    // Helper functions for logging
    const log = typeof NestedInteger !== 'undefined' ? () => {} : console.log;
    const table = typeof NestedInteger !== 'undefined' ? () => {} : console.table;

    /**
     * @param {number} num original
     * @returns {number} mapped
     */
    function conv(num) {
        return +String(num).split('').map(ch => mapping[+ch]).join('');
    }

    const pairs = nums.map(n => [n, conv(n)]);
    log('\npairs:');
    table(pairs);

    const sortedPairs = [...pairs].sort((a, b) => a[1] - b[1]);
    log('\nsortedPairs:');
    table(sortedPairs);

    const result = sortedPairs.map(p => p[0]);
    log('\nresult:');
    table(result);

    return result;
}

// Test cases
const testCases = [
    { mapping: [2,1,4,8,6,3,0,9,7,5], nums: [990, 332, 981], expected: [990, 981, 332] },
//    { mapping: [0,1,2,3,4,5,6,7,8,9], nums: [12, 21, 3], expected: [3, 12, 21] },
//    { mapping: [9,8,7,6,5,4,3,2,1,0], nums: [12, 34, 56, 78], expected: [78, 56, 34, 12] },
//    { mapping: [1,0,3,2,6,4,5,9,7,8], nums: [101, 200, 123, 241], expected: [123, 241, 101, 200] },
//    { mapping: [5,4,3,2,1,0,9,8,7,6], nums: [500, 432, 990, 321], expected: [432, 321, 500, 990] }
];

testCases.forEach((test, index) => {
    const result = sortJumbledNumbers(test.mapping, test.nums);
    console.log(`Test Case ${index + 1}: ${result.toString() === test.expected.toString() ? 'Passed' : 'Failed'} (Expected: ${test.expected}, Got: ${result})`);
});

Output sample


pairs:
┌─────────┬─────┬─────┐
│ (index) │ 0   │ 1   │
├─────────┼─────┼─────┤
│ 0       │ 990 │ 552 │
│ 1       │ 332 │ 884 │
│ 2       │ 981 │ 571 │
└─────────┴─────┴─────┘

sortedPairs:
┌─────────┬─────┬─────┐
│ (index) │ 0   │ 1   │
├─────────┼─────┼─────┤
│ 0       │ 990 │ 552 │
│ 1       │ 981 │ 571 │
│ 2       │ 332 │ 884 │
└─────────┴─────┴─────┘

result:
┌─────────┬────────┐
│ (index) │ Values │
├─────────┼────────┤
│ 0       │ 990    │
│ 1       │ 981    │
│ 2       │ 332    │
└─────────┴────────┘
Test Case 1: Passed (Expected: 990,981,332, Got: 990,981,332)
undefined
© d)zharii. sitemap