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

Поділитися
Вставка
  • Опубліковано 11 жов 2024

КОМЕНТАРІ • 37

  • @LEETCODESOLVER-in3wz
    @LEETCODESOLVER-in3wz 6 місяців тому +11

    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 7 місяців тому

      Agree very much !

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

  • @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 7 місяців тому

      nt

  • @sionkim5504
    @sionkim5504 Рік тому +2

    excellent idea. please keep up daily viedos!

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

  • @krateskim4169
    @krateskim4169 Рік тому +3

    Awesome content as usual

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

  • @omaryahia
    @omaryahia 3 місяці тому

    this is mind opening 👌🏻✨👏🏻 thank you

  • @adwaitkesharwani3569
    @adwaitkesharwani3569 Рік тому +1

    Excellent explanation!

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

  • @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 7 місяців тому

      we can have values

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

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

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

  • @shwetanksingh5438
    @shwetanksingh5438 Рік тому +1

    Awesome

  • @wzct
    @wzct 5 місяців тому

    you lost me at bit manipulation