Sort the Jumbled Numbers - Leetcode 2191 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.พ. 2025

ความคิดเห็น • 34

  • @jeffhappens1
    @jeffhappens1 6 หลายเดือนก่อน +1

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

  • @durgeshnandan5181
    @durgeshnandan5181 6 หลายเดือนก่อน +2

    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

  • @33galactus
    @33galactus 6 หลายเดือนก่อน +1

    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
      @cinimodmil 6 หลายเดือนก่อน

      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 .

    • @reactionchamber
      @reactionchamber 6 หลายเดือนก่อน

      @@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

    • @33galactus
      @33galactus 6 หลายเดือนก่อน

      @@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))
      ```

  • @abhinavchoukse2357
    @abhinavchoukse2357 6 หลายเดือนก่อน

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

  • @anandsrikumar007
    @anandsrikumar007 6 หลายเดือนก่อน

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

  • @chien-yuyeh9386
    @chien-yuyeh9386 6 หลายเดือนก่อน +1

    Thanks for sharing!! 🙏

  • @nolanstutelberg3900
    @nolanstutelberg3900 6 หลายเดือนก่อน

    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

  • @yang5843
    @yang5843 6 หลายเดือนก่อน +12

    Leetcode is really milking these kinda problems

  • @rahulsbhatt
    @rahulsbhatt 6 หลายเดือนก่อน

    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.

  • @groznee
    @groznee 6 หลายเดือนก่อน

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

  • @fastrafool
    @fastrafool 6 หลายเดือนก่อน

    Really need to finish your python course😭

  • @JamesBond-mq7pd
    @JamesBond-mq7pd 6 หลายเดือนก่อน

    wow. mod and div faster than strings!

  • @p_k_repswal
    @p_k_repswal 6 หลายเดือนก่อน +1

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

    • @LAFLAME1111
      @LAFLAME1111 6 หลายเดือนก่อน +1

      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.

  • @jayrathod9271
    @jayrathod9271 6 หลายเดือนก่อน

    I faced the same error. 12:04 😆

  • @saishivamgupta4584
    @saishivamgupta4584 6 หลายเดือนก่อน

    Please do solution video for Leetcode 827 making a large island

  • @rahulsihara8946
    @rahulsihara8946 6 หลายเดือนก่อน

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

  • @v0nnyboy
    @v0nnyboy 6 หลายเดือนก่อน +1

    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 .

    • @chrischika7026
      @chrischika7026 6 หลายเดือนก่อน

      you are correct which is why you can do it in on line

  • @arknite9278
    @arknite9278 6 หลายเดือนก่อน

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

    • @takeuchi5760
      @takeuchi5760 6 หลายเดือนก่อน

      Because n is a number. And constraints say the number is

  • @selfhelpguy5589
    @selfhelpguy5589 6 หลายเดือนก่อน

    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

    • @funnyhell14321
      @funnyhell14321 6 หลายเดือนก่อน +1

      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.

    • @rahulsbhatt
      @rahulsbhatt 6 หลายเดือนก่อน +1

      @@funnyhell14321 agreed!

  • @arihannnt
    @arihannnt 6 หลายเดือนก่อน +1

    they probably did it on purpose :P!

  • @mohanedomer9081
    @mohanedomer9081 6 หลายเดือนก่อน

    first comment i think

  • @aadharjain313
    @aadharjain313 6 หลายเดือนก่อน

    Bro is getting more dark by the day💀💀

  • @Ivoshevo
    @Ivoshevo 6 หลายเดือนก่อน

    LEET-CODE is dead