i followed a slightly different approach , taking a dictionary instead of pairs with key as the mapping of that number and value as a list , when we traverse linearly in nums we maintain the order. def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]: def getMapped(num): mapped_num = 0 multiplier = 1 if num == 0: return mapping[0] while num > 0: num, digit = divmod(num, 10) mapped_num += mapping[digit] * multiplier multiplier *= 10 return mapped_num dic = defaultdict(list) for num in nums: dic[getMapped(num)].append(num) keys = sorted(dic.keys()) res = [] for key in keys: res.extend(dic[key]) return res
Great explanation! I did it a slightly different way based on your previous video. I used the Python sort function as follows: ``` return sorted(nums, key=lambda x: getMappedNumber(x)) ``` where getMappedNumber() is a helper function to get a mapped value of a number. Also, indices are inherently sorted in the Python sort function, so we do not have to mention them in the lambda function. And we are not modifying the input array as we used sorted(nums) instead nums.sort().
@@cinimodmil Here is my code. ``` def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]: def getMappedNumber(num): if num == 0: return mapping[0] new_num = 0 base = 1 while num: num, d = num // 10, num % 10 d = mapping[d] * base new_num += d base *= 10 return new_num return sorted(nums, key=lambda x:getMappedNumber(x)) ```
I used a minheap to store (mappedNum, original Index, originalNum) and then just popped off and appended to list. And I converted the num to a string to iterate over it and appended all the numbers to create mappedNum... Then converted to an integer before the heap to remove the leading zeros
Thanks for sharing! The problem description wasn't that good for me, I couldn't understand that it was a decimal system array, you explained that and things clicked for me.
When they say it's medium but it's a one-liner. return [nums[tupple[1]] for tupple in sorted( [ (int(''.join(str(mapping[int(digit)]) for digit in str(num))), indeks) for indeks, num in enumerate(nums) ] )]
i is the index of the nums array and it is used for the ties. In python, to append multiple values to an index you have to wrap it in a tuple since the append function only takes one object.
Can anyone break down the time complexity for the code shown here? I'm not clear why we don't consider the time complexity of iteration of the digits in every integer (the inner loop).
These questions are too easy in Python compared to Java. I hate the comparator shit in java. The problem is what if the interviewer asks me how will you implement it without this thing that you've used in python. I would love to know Sir Navdeep's opinion on this
I don't understand your question. But, if you solve this problem in Python in a coding interview, the interviewer won't ask you to code it without any default function unless the interviewer is in a bad mood or something.
I figured this one by myself! Yay! Thank you for making these videos! They help me get into the mindset of solving these problems!
i followed a slightly different approach , taking a dictionary instead of pairs with key as the mapping of that number and value as a list , when we traverse linearly in nums we maintain the order.
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
def getMapped(num):
mapped_num = 0
multiplier = 1
if num == 0:
return mapping[0]
while num > 0:
num, digit = divmod(num, 10)
mapped_num += mapping[digit] * multiplier
multiplier *= 10
return mapped_num
dic = defaultdict(list)
for num in nums:
dic[getMapped(num)].append(num)
keys = sorted(dic.keys())
res = []
for key in keys:
res.extend(dic[key])
return res
Great explanation!
I did it a slightly different way based on your previous video. I used the Python sort function as follows:
```
return sorted(nums, key=lambda x: getMappedNumber(x))
```
where getMappedNumber() is a helper function to get a mapped value of a number.
Also, indices are inherently sorted in the Python sort function, so we do not have to mention them in the lambda function.
And we are not modifying the input array as we used sorted(nums) instead nums.sort().
hi! could you please show getMappedNumber? i did it this way: res = sorted(nums, key=lambda num: (mask[num], pos[num])) where mask is , pos is .
@@cinimodmil
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
def custSort(x):
s = ''
for k in str(x):
s+=str(mapping[int(k)])
return int(s)
nums.sort(key=custSort)
return nums
@@cinimodmil
Here is my code.
```
def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
def getMappedNumber(num):
if num == 0:
return mapping[0]
new_num = 0
base = 1
while num:
num, d = num // 10, num % 10
d = mapping[d] * base
new_num += d
base *= 10
return new_num
return sorted(nums, key=lambda x:getMappedNumber(x))
```
I was so confused about the error that came at 11:17 but you resolved it...Thanks 😁
I solved it today. It felt easy. I took some extra space complexity.
Thanks for sharing!! 🙏
I used a minheap to store (mappedNum, original Index, originalNum) and then just popped off and appended to list. And I converted the num to a string to iterate over it and appended all the numbers to create mappedNum... Then converted to an integer before the heap to remove the leading zeros
Leetcode is really milking these kinda problems
Thanks for sharing!
The problem description wasn't that good for me, I couldn't understand that it was a decimal system array, you explained that and things clicked for me.
When they say it's medium but it's a one-liner.
return [nums[tupple[1]] for tupple in sorted( [
(int(''.join(str(mapping[int(digit)]) for digit in str(num))), indeks)
for indeks, num in enumerate(nums)
] )]
Really need to finish your python course😭
wow. mod and div faster than strings!
I didn't understand what is the role of i in pairs.append((mapped_n, i)) and why 2 parentheses are used?
i is the index of the nums array and it is used for the ties.
In python, to append multiple values to an index you have to wrap it in a tuple since the append function only takes one object.
I faced the same error. 12:04 😆
Please do solution video for Leetcode 827 making a large island
Used hasmap to solve it. Where original number is key.
Correct me if i'm wrong , but as far as i know , the sort in python is stable by default .... so adding the index param is redundant .
you are correct which is why you can do it in on line
Can anyone break down the time complexity for the code shown here? I'm not clear why we don't consider the time complexity of iteration of the digits in every integer (the inner loop).
Because n is a number. And constraints say the number is
These questions are too easy in Python compared to Java. I hate the comparator shit in java. The problem is what if the interviewer asks me how will you implement it without this thing that you've used in python.
I would love to know Sir Navdeep's opinion on this
I don't understand your question. But, if you solve this problem in Python in a coding interview, the interviewer won't ask you to code it without any default function unless the interviewer is in a bad mood or something.
@@funnyhell14321 agreed!
they probably did it on purpose :P!
first comment i think
Bro is getting more dark by the day💀💀
LEET-CODE is dead