Shuffle the Array (Constant Space) - Leetcode 1470 - Python

Поделиться
HTML-код
  • Опубликовано: 18 окт 2024

Комментарии • 38

  • @LEETCODESOLVER-in3wz
    @LEETCODESOLVER-in3wz 7 месяцев назад +12

    the actual problem is easy
    but implementing it ate my brain

  • @santanu29
    @santanu29 Год назад +8

    That was a really smart solution. Never would have thought in this way.

  • @MP-ny3ep
    @MP-ny3ep Год назад +8

    Really smart solution. These daily problems are helping a lot. Thank you

  • @shejutikarmakar707
    @shejutikarmakar707 Год назад +17

    Instead of using bit manipulation we can store x,y at the same place using nums[i]*1001+nums[i+n]. Because all the numbers are less than or equal to 1000.Then we will start traversing from end .Will get y part using nums[i]%1001 and x part using nums[i]/1001.

    • @amitroe7641
      @amitroe7641 Год назад +6

      bit operations as faster then arithmetic operations ☮

    • @nehaa3778
      @nehaa3778 6 месяцев назад +1

      Ya but easier to remember for some.

  • @jammy2003
    @jammy2003 Год назад +12

    This should be a medium-level problem lol. Definitely not easy (at least for me) to solve this in O(1) space complexity.

    • @jst8922
      @jst8922 8 месяцев назад

      Agree very much !

  • @CostaKazistov
    @CostaKazistov Год назад +13

    Brilliant walkthrough of today's Leetcode challenge. As always.
    Question: Why not use `reversed(range(n))` in the for loop instead of (n-1, -1, -1)?
    Works the same, doesn't it?

    • @NeetCodeIO
      @NeetCodeIO  Год назад +8

      Yeah, that's definitely a cleaner way of writing it, will probably do it that way in the future!

    • @AndreiSokolov-k7j
      @AndreiSokolov-k7j 8 месяцев назад

      nt

  • @WilcoVerhoef
    @WilcoVerhoef Год назад +1

    It feels a bit cheaty to use 32bit ints while 16bit would've been enough, that's basically hiding an output array in plain sight from the start.
    It's even possible to do in place permutations without the assumption that half of the bits are unused; by following the cycles:
    for t in 0 to 2n:
    s = source(t)
    while s < t: s = source(s)
    swap(s,t)
    def source(t): if t even then t/2 else t/2+n

  • @sionkim5504
    @sionkim5504 Год назад +2

    excellent idea. please keep up daily viedos!

  • @omaryahia
    @omaryahia 3 месяца назад

    this is mind opening 👌🏻✨👏🏻 thank you

  • @krateskim4169
    @krateskim4169 Год назад +3

    Awesome content as usual

  • @paras9392
    @paras9392 Год назад +1

    Another simple 3 line python code to solve it in O(1) space:
    for i in range(n):
    nums[ 2 * i + 1 : 2 * i + 1 ]=[nums.pop( n + i )]
    return nums
    Here, nums[i:i] inserts any iterable (e.g. [2]) at ith index by shifting the rest of the elements right.
    However bit operations are computationally much more efficient, whereas here we have to shift elements to insert other elements.

    • @insertfahim
      @insertfahim Год назад

      Best

    • @stoic12343
      @stoic12343 6 дней назад

      pop in the middle of an array is not O(1) , the shifting is O(n)

  • @vuejs1
    @vuejs1 3 месяца назад

    fantastic, at this poit it is more art then code

  • @kumarc4853
    @kumarc4853 5 месяцев назад +1

    interviewers which expect people to solve this in o(1) space are just testing your memory / luck

  • @adwaitkesharwani3569
    @adwaitkesharwani3569 Год назад +1

    Excellent explanation!

  • @satwiktatikonda764
    @satwiktatikonda764 Год назад

    i think creating a array and returning the array won't cause any space complexity because i/p and o/p space are counted right? then first solution is also o(1) .............SC correct if I am wrong

  • @edenrosales8214
    @edenrosales8214 Год назад +15

    Why do you not advertise this channel on your main? I feel like these videos would get more views if I had known that it existed other than getting them randomly recommended to me.

  • @harshitpant07
    @harshitpant07 Год назад +3

    Did not get the part
    y = nums[ i ] & (2**10 -1)
    I do know that we have to move y back to the end of the list but how is that helping here?

    • @shikharcodes
      @shikharcodes Год назад +2

      We are doing AND operation here to extract back the value of Y from the XY pair. Now, we know that in bits, we represent 2^0 as 1, 2^1 as 10, 2^2 as 100, and so on. Now, we want ten 1's so that we can AND it with the Y in XY(Y is the last ten digits in XY). For getting ten 1's we would do 2^10, but this would give 10000000000 (total 11 digits, one 1 and ten 0s), but we want ten 1's i.e. 1111111111, and hence we do 2^10-1 i.e. 2**10-1.

    • @AndreiSokolov-k7j
      @AndreiSokolov-k7j 8 месяцев назад

      we can have values

  • @servantofthelord8147
    @servantofthelord8147 3 месяца назад

    Is this constant space? :
    def shuffle(self, nums: List[int], n: int) -> List[int]:
    for i in range(n):
    nums[i] = (nums[i],nums[i+n])
    for i in range(n-1,-1,-1):
    nums[2*i],nums[(2*i)+1] = nums[i][0],nums[i][1]
    return nums

    • @servantofthelord8147
      @servantofthelord8147 3 месяца назад

      Oh wait, nvm. The introduction of tuples here means that our program allocates more space for those tuples as the size of our input grows (since we'll have n tuples). So the memory complexity is linear. I see exactly why we used the bitwise approach now.

    • @servantofthelord8147
      @servantofthelord8147 3 месяца назад

      Here's my revised solution : def shuffle(self, nums: List[int], n: int) -> List[int]:
      for i in range(n):
      nums[i] 10
      y = nums[i] & ((2**10)-1)
      nums[2*i],nums[(2*i)+1] = x,y
      return nums

  • @jshaikh3897
    @jshaikh3897 4 месяца назад

    H
    'm sorry if this sounds silly, but when you say 32 bit, does it mean nums[0] is 32 bit , nums [ 1] is 32 bit and so on?
    also I didn't really understand the 10 bits part, like how is one array value 10 bits?
    Can someone please explain. I feel like I'm either unable to apply the basic concepts here or missing some basic information

    • @coderSandeep
      @coderSandeep 2 месяца назад

      yes each number is occupying 32 bits and 10 bit is the actual number that occupying 10 bits and other bits are just filled with zeroes

  • @abdifatahmoh
    @abdifatahmoh Год назад +1

    Simple solution:
    res = []
    for i in range(n):
    curr_first_half = nums[i]
    curr_2nd_half = nums[n + i]
    res.append(curr_first_half)
    res.append(curr_2nd_half)
    return res

    • @oscar5915
      @oscar5915 5 месяцев назад

      lol this take o(n) space complexity instead of constant

  • @shwetanksingh5438
    @shwetanksingh5438 Год назад +1

    Awesome

  • @wzct
    @wzct 5 месяцев назад

    you lost me at bit manipulation