Sponsored
Sponsored
Explanation: To sort the numbers in 'nums' according to their mapped values, we'll first calculate the mapped value for each number. We can achieve this by creating a helper function that translates each digit using the given 'mapping' array. Then, we can pair each original number with its mapped value as tuples and sort these tuples based on the mapped values. This ensures that the relative order is maintained for numbers with the same mapped value.
Time Complexity: O(n * k log k), where n is the number of elements in 'nums' and k is the average number of digits in the numbers. This complexity arises from mapping each digit (O(k)) and sorting (O(n log n)).
Space Complexity: O(n) for storing the tuples of numbers and their mapped values.
1function sortJumbled(mapping, nums) {
2 const mappedValue = (num) => {
3 return parseInt(num.toString().split('').map(digit => mapping[digit]).join(''), 10);
4 };
5
6 return nums.map(num => [num, mappedValue(num)])
7 .sort((a, b) => a[1] - b[1])
8 .map(pair => pair[0]);
9}
10
11// Example usage:
12const mapping = [0,1,2,3,4,5,6,7,8,9];
13const nums = [789,456,123];
14console.log(sortJumbled(mapping, nums)); // Output: [123, 456, 789]
In this JavaScript solution, a mappedValue
function converts a number into its mapped equivalent using the 'mapping'. We transform 'nums' into an array of tuples containing each number and its mapped value, then sort these tuples by the mapped values, and finally map the sorted tuples back to just an array of numbers.
Explanation: Instead of creating tuples, we can use a custom comparator function directly in our sort operation. This comparator will determine the order based on mapped values of numbers. By transforming the comparator function, we ensure in-place sorting, minimizing additional memory usage.
Time Complexity: O(n * k log k), where n is the number of numbers and k is the average number of digits in the numbers due to the sorting and mapping logic. Space Complexity: O(1) additional in-place space.
1#include <iostream>
2#include <vector>
3#include <string>
4#include <algorithm>
5
int mappedValue(int num, const std::vector<int>& mapping) {
std::string str = std::to_string(num);
for (char& c : str) {
c = '0' + mapping[c - '0'];
}
return std::stoi(str);
}
std::vector<int> sortJumbled(const std::vector<int>& mapping, std::vector<int>& nums) {
std::sort(nums.begin(), nums.end(), [&mapping](int a, int b) {
return mappedValue(a, mapping) < mappedValue(b, mapping);
});
return nums;
}
int main() {
std::vector<int> mapping = {8,9,4,0,2,1,3,5,7,6};
std::vector<int> nums = {991,338,38};
std::vector<int> result = sortJumbled(mapping, nums);
for (int num : result) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
In this C++ solution, the mappedValue
function calculates the mapped equivalent of a number using the 'mapping'. The comparator for the std::sort
uses this mapped value to sort the numbers in-place. The syntax makes use of C++ lambda functions for inline declaration of comparator logic.