1089. Duplicate Zeros - LeetCode Solution

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.
    Note that elements beyond the length of the original array are not written.
    Do the above modifications to the input array in place, do not return anything from your function.
    Example 1:
    Input: [1,0,2,3,0,4,5,0]
    Output: null
    Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
    LeetCode Problem: leetcode.com/p...
    GitHub Solution: github.com/Raj...
    --------------------------------------------------------------------------------------------------------------
    Subscribe to my other TH-cam channel for a peek in the life:
    ☞ Chai & Waffle: / @chaiwaffle7181
    --------------------------------------------------------------------------------------------------------------
    Follow my daily stories on Instagram:
    📸 Rajdeep Kaur: @_rajdeepkaur || / _rajdeepkaur
    --------------------------------------------------------------------------------------------------------------
    Join me on:
    🙌 Facebook: / chai-waffle-1067593849...
    📩 E-mail: rajdeepkaur245@gmail.com
    --------------------------------------------------------------------------------------------------------------
    Hi Everyone, I'm Rajdeep Kaur and I'm an ex-Amazon software engineer. As of now, I am working as a Software Engineer in Miro. I was hired directly from India to Europe (Netherlands).
    I make videos on competitive programming, Data Structures and Algorithms and tips to get hired.
    Subscribe for the latest updates, I upload new videos almost everyday :) I hope my content helps you in getting your dream job.

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

  • @gsb22
    @gsb22 4 ปีที่แล้ว +13

    I wasted 1 day to understand the solution given on LC and got fed up. Glad I found your video. It's simple, and not confusing.

  • @SalviPapiii
    @SalviPapiii 3 ปีที่แล้ว +7

    I want to let you know that it took me almost 3 days to figure it out since I haven't coded in a couple months... Thank you so much for this! It really helped refresh my memory a lot.

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว

      Glad that it helped you. Your comment really motivated me. Please subscribe to my channel to see my latest content.

  • @gangster4069
    @gangster4069 2 ปีที่แล้ว +5

    This solution has a problem, if your input array's last element is zero, this solution will fail. input array's last element with zero is an boundary condition, to handle it decremenet count of zeros by 1, if the last element in the array is zero.

  • @nandanimadhukar
    @nandanimadhukar 2 ปีที่แล้ว +2

    easy explanation to what seemed like a twisted but marked "easy"!

  • @satyamgupta6030
    @satyamgupta6030 ปีที่แล้ว

    crystal clear explaination and concise code.
    thanks for the solution.

  • @huseynmikayil6354
    @huseynmikayil6354 ปีที่แล้ว +1

    Here is the TypeScript solution without additional insert function:
    const duplicateZeros = function (nums: number[]) {
    let zeroes: number = 0;
    for (let i = 0; i < nums.length; i++) {
    if (nums[i] === 0) zeroes++;
    }
    let i = nums.length - 1,
    j = nums.length - 1 + zeroes;
    while (i !== j) {
    if (j < nums.length) nums[j] = nums[i];
    j--;
    if (nums[i] === 0) {
    if (j < nums.length) nums[j] = nums[i];
    j--;
    }
    i--;
    }
    };

  • @pradeepanbaluchamy9503
    @pradeepanbaluchamy9503 4 ปีที่แล้ว +2

    Good and simple explanation. Thanks

  • @MunniDivya
    @MunniDivya 4 ปีที่แล้ว +1

    can u please tell me how did u get this ideA?

  • @rohanjuneja1778
    @rohanjuneja1778 2 ปีที่แล้ว +1

    not of use. only algo.no intuition.

  • @mohanaravind7656
    @mohanaravind7656 4 ปีที่แล้ว +1

    Sorry I might be missing something, but I see a `i--` in the code, but not a `j--` when going through the array. How is `j` decremented?

    •  3 ปีที่แล้ว +1

      When the insert function is called, she is passing j-- as argument, the -- operator will decrement the variable value itself, not only the value being passed to the function.

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว

      Your comment really motivates me. Please subscribe to my channel to see my latest content.

  • @MunniDivya
    @MunniDivya 3 ปีที่แล้ว +1

    why did u decrement j pointer when i==0 . I dint understand

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว

      I hope you figured it out. Please subscribe to my channel to see my latest content.

  • @putinvladimir420
    @putinvladimir420 2 ปีที่แล้ว

    this code fails if zero is present at the last index.

  • @user-cl3un2qx7s
    @user-cl3un2qx7s 5 หลายเดือนก่อน

    please explain the maths behind your code / trick !

  • @HarshitMaurya
    @HarshitMaurya 2 ปีที่แล้ว

    Best Explanation

  • @saikirankorri2180
    @saikirankorri2180 3 ปีที่แล้ว +1

    Your explanation was good .. thank you

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว

      Glad that you liked it. Please subscribe to my channel to see my latest content.

  • @joscandynunez2169
    @joscandynunez2169 ปีที่แล้ว

    how is this marked easy ?

  • @Nature-pb9dh
    @Nature-pb9dh ปีที่แล้ว

    Thank you this is great!

  • @himanshuchhikara4918
    @himanshuchhikara4918 2 ปีที่แล้ว +1

    very helpful solution

  • @kyotang8657
    @kyotang8657 3 ปีที่แล้ว

    Hi there, would you please explain how you have arrive the condition for the pointer decrements, especially for the case where when the array element is zero we would copy the element to the index of j then decrement j... and give you this example [8,4,5,0,0,7]... I can’t seem to wrap my head around it using the two pointer approach

    • @kyotang8657
      @kyotang8657 3 ปีที่แล้ว +2

      Never mind when I stepped through I realize that when you have consecutive Zeros, the termination point of the while loop actually happens at the index of the very next left element .... this is so smart use of two pointers as j will be decremented before i, then i gets decremented then the while loop termination condition will be met

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว

      Glad that you figured. Please subscribe to my channel to see my latest content.

  • @chankwongyin7455
    @chankwongyin7455 2 ปีที่แล้ว

    very well explained!

  • @janhavibhosle683
    @janhavibhosle683 4 ปีที่แล้ว +1

    Why didn't you shift i till first element?

    • @pradeepanbaluchamy9503
      @pradeepanbaluchamy9503 4 ปีที่แล้ว +1

      Watch it again. There she mentioned I=j and hence she stopped the shifting.

    • @gameofcoders1036
      @gameofcoders1036  4 ปีที่แล้ว +1

      Yes, once i and j becomes equal, we know that all items are at correct places. So no computation needed.

    • @nirajchowdhary7372
      @nirajchowdhary7372 3 ปีที่แล้ว

      because after this point i and j will always point at same element, so it would be always swapping element with itself till the first element

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว +1

      Please subscribe to my channel to see my latest content.

  • @jorgegonzaloalfaro5378
    @jorgegonzaloalfaro5378 3 ปีที่แล้ว

    insert operation is O(n) time complexity no? so worst case scenario would be o(n^2) . if you disagree can you explain why this is the case?

    • @AdrianGonzalezBlogs
      @AdrianGonzalezBlogs 3 ปีที่แล้ว +1

      Insert operation is O(1) since it only swaps the array elements values

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว

      Your comment really motivates me. Please subscribe to my channel to see my latest content.

  • @shuchitam3868
    @shuchitam3868 4 ปีที่แล้ว +1

    can you explain the run time of the solution as well?

    • @gameofcoders1036
      @gameofcoders1036  4 ปีที่แล้ว +2

      Hi Gayatri, Time complexity for solution covered in video will be O(n), as we are traversing the array just once.

    • @shuchitam3868
      @shuchitam3868 4 ปีที่แล้ว

      @@gameofcoders1036 Thanks!

  • @systemforge
    @systemforge 2 ปีที่แล้ว

    woah!.. nice

  • @hhuynh8195
    @hhuynh8195 2 ปีที่แล้ว

    your solution might work but I don't understand your explanation at all. T.T I'm sorry I try but the docs you used to explain your idea doesn't really help with visualization. Honestly it actually confuse me more.

  • @MunniDivya
    @MunniDivya 4 ปีที่แล้ว

    is there any algorithm already present?

    • @jasjeetsingh1967
      @jasjeetsingh1967 4 ปีที่แล้ว

      There are other algorithms as well, and they are available on different websites!

    • @gameofcoders1036
      @gameofcoders1036  4 ปีที่แล้ว +1

      Yes that's right, you can find different algorithms from other websites.

    • @gameofcoders1036
      @gameofcoders1036  3 ปีที่แล้ว

      Please subscribe to my channel to see my latest content.

  • @speedbreaker5642
    @speedbreaker5642 4 ปีที่แล้ว +1

    Good one.

  • @oleksii7691
    @oleksii7691 2 ปีที่แล้ว +1

    Nice solution but awful explanation.

  • @Abhishek-Khelge
    @Abhishek-Khelge 4 ปีที่แล้ว +1

    I have used ArrayList this is easier this way
    List al =new ArrayList();
    for(int k : arr){
    al.add(k);
    }

    boolean flag=false;
    for(int i=0;i