Info
I would say that a faster solution exists since you can utilize the fact that you don’t have to map every number. Rather, you can just only convert those numbers you need to, such as the initial 9, and since that would be greater than all the numbers, you no longer would have to process that number and can slot it at the end. However, that would not effect the worst case runtime.
Brute Force Solution
- Simply map all the numbers first, then sort them, then remap based on key
class Solution:
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
d = {}
for i, num in enumerate(nums):
changed_num = []
for integer in str(num):
changed_num.append(str(mapping[int(integer)]))
d[i] = int(''.join(changed_num))
sorted_keys = sorted(d, key=d.get)
res = []
for key in sorted_keys:
res.append(nums[key])
return res