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.
1def sortJumbled(mapping, nums):
2 def mapped_value(num):
3 return int(''.join(str(mapping[int(digit)]) for digit in str(num)))
4
5 return [num for num, _ in sorted((num, mapped_value(num)) for num in nums, key=lambda x: x[1])]
6
7# Example usage:
8mapping = [8,9,4,0,2,1,3,5,7,6]
9nums = [991,338,38]
10print(sortJumbled(mapping, nums)) # Output: [338, 38, 991]
The code defines a function mapped_value
that converts a given number into its mapped equivalent using the 'mapping' array. We iterate through each number in 'nums', pair it with its mapped value, and sort these pairs based on the mapped values (using a lambda function as the key for sorting). Finally, we extract and return just the numbers in their new order.
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.