Sort the Jumbled Numbers - Leetcode 2191 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
4:45 - Coding Explanation
8:36 - Coding Explanation 2

leetcode 2191

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Leetcode is really milking these kinda problems

yang
Автор

I figured this one by myself! Yay! Thank you for making these videos! They help me get into the mindset of solving these problems!

jeffhappens
Автор

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:

keys = sorted(dic.keys())
res = []
for key in keys:
res.extend(dic[key])
return res

durgeshnandan
Автор

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().

galactus
Автор

I was so confused about the error that came at 11:17 but you resolved it...Thanks 😁

abhinavchoukse
Автор

I solved it today. It felt easy. I took some extra space complexity.

anandsrikumar
Автор

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.

rahulsbhatt
Автор

Really need to finish your python course😭

fastrafool
Автор

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

nolanstutelberg
Автор

I didn't understand what is the role of i in pairs.append((mapped_n, i)) and why 2 parentheses are used?

p_k_repswal
Автор

When they say it's medium but it's a one-liner.

return [nums[tupple[1]] for tupple in sorted( [
for digit in str(num))), indeks)
for indeks, num in enumerate(nums)
] )]

groznee
Автор

Please do solution video for Leetcode 827 making a large island

saishivamgupta
Автор

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 .

vnnyboy
Автор

Used hasmap to solve it. Where original number is key.

rahulsihara
Автор

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).

arknite
Автор

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

selfhelpguy