Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Sort the Jumbled Numbers

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2191" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system. The mapped value 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. You are also given another integer array nums. Return the array nums sorted in non-decreasing order based on the mapped values of its elements. Notes: Elements with the same mapped values should appear in the same relative order as in the input. The elements of nums should only be sorted based on their mapped values and not be replaced by them. Example 1: Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38] Output: [338,38,991] Explanation: Map the number 991 as follows: 1. mapping[9] = 6, so all occurrences of the digit 9 will become 6. 2. mapping[1] = 9, so all occurrences of the digit 1 will become 9. Therefore, the mapped value of 991 is 669. 338 maps to 007, or 7 after removing the leading zeros. 38 maps to 07, which is also 7 after removing leading zeros. Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38. Thus, the sorted array is [338,38,991]. Example 2: Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123] Output: [123,456,789] Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789]. Constraints: mapping.length == 10 0 <= mapping[i] <= 9 All the values of mapping[i] are unique. 1 <= nums.length <= 3 * 104 0 <= nums[i] < 109

Explanation

Here's a breakdown of the solution approach, followed by the Python code:

  • Map and Sort: Create a mapping of each number in nums to its mapped value. Then, sort the original nums array based on these mapped values, preserving the original order for elements with equal mapped values.
  • Efficient Mapping: Implement a function to efficiently calculate the mapped value of a number using the provided mapping array.
  • Stable Sorting: Use a stable sorting algorithm to ensure that elements with the same mapped values retain their original order in nums.

  • Runtime Complexity: O(n log n) due to sorting, where n is the length of the nums array. The mapping operation for each number takes O(log(nums[i])) time which is dominated by O(n log n).

  • Storage Complexity: O(n) to store the mapped values of numbers.

Code

    def sort_by_mapping(mapping, nums):
    """
    Sorts an array of integers based on their mapped values according to a given mapping.

    Args:
        mapping: A list of integers representing the mapping rule.
        nums: A list of integers to be sorted.

    Returns:
        A list of integers sorted based on their mapped values.
    """

    def map_value(num):
        """
        Calculates the mapped value of a number.

        Args:
            num: The number to be mapped.

        Returns:
            The mapped value of the number.
        """
        s_num = str(num)
        mapped_s = ""
        for digit in s_num:
            mapped_s += str(mapping[int(digit)])

        # Handle leading zeros by converting to an integer then back to a string
        if mapped_s:
          return int(mapped_s)
        else:
          return 0


    # Create a list of tuples (num, mapped_value)
    mapped_nums = [(num, map_value(num)) for num in nums]

    # Sort the list of tuples based on the mapped values, preserving original order
    mapped_nums.sort(key=lambda x: x[1])

    # Extract the sorted numbers from the tuples
    sorted_nums = [num for num, _ in mapped_nums]

    return sorted_nums

More from this blog

C

Chatmagic blog

2894 posts