Maximum Consecutive Ones II (If we can flip at most one 0)

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 34

  • @rajeshsunarkani1063
    @rajeshsunarkani1063 Před 4 lety +6

    thanks for the clear explanation,now I understood how to implement sliding window technique in linear time

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  Před 4 lety +1

      Great to hear!

    • @karan150191
      @karan150191 Před 2 lety

      Hi Can you explain how is this linear time? I see a while loop inside the for loop which makes it Quadratic time.?

    • @rc_woshimao957
      @rc_woshimao957 Před rokem

      @@karan150191 This might be too late, but I think one possible explanation is that the time of execution of the while loop is not related to the actual length of the array. When talking about o(n), we are saying if we take n as the length of the array, the general time execution is o(n); the while loop condition zeroCount > k is relative to the number of zero count & k, which can be trivial when met with an arbitrary sized array. But I can be wrong, I have just been taught this way. (Or maybe its just amortized O(n).)

  • @deepaklobaniya8701
    @deepaklobaniya8701 Před 2 lety +1

    but one thing please can you explain how we can return that
    index that we made 0 to 1 .

  • @danielaviv8215
    @danielaviv8215 Před rokem

    you can do this code instead, for me it was much easier:
    int last0 = -1;
    int left, right, max;
    for (left = right = max = 0; right < n; ++i) {
    if (arr[right] == 0) {
    if (left

  • @bchen1403
    @bchen1403 Před 2 lety +1

    Great code.

  • @karan150191
    @karan150191 Před 2 lety +2

    Hi, Thank you for the video, can you please explain why you did end-start+1(why +1) in the last line of the loop?

    • @lukebrichey
      @lukebrichey Před 2 lety +1

      This may be a bit late, but that is the comparison between the previous longest sequence and then the current length of the sequence (distance from pointer 1 to pointer 2 + 1 to get the length)

  • @Imgahlot
    @Imgahlot Před 4 lety

    good one..very easy to understand.

  • @paragroy5359
    @paragroy5359 Před 3 lety

    Nice explanation....

  • @comedycentral4333
    @comedycentral4333 Před 3 lety

    The code is not returning the correct result for this input "1110" k = 1 please check this case

  • @durgaprasadsurada4643
    @durgaprasadsurada4643 Před 2 lety

    It is not working for input 11100 and 01, could you please check once

  • @abulhashemjr8999
    @abulhashemjr8999 Před 4 lety

    The point of two pointer is to improve performance! Your Algorithm yields O(n2) time complexity!

  • @shubhamkhuntia2412
    @shubhamkhuntia2412 Před 2 lety

    Question link to solve ?

  • @soniabharti3304
    @soniabharti3304 Před 3 lety +1

    Nice explanation 👌 but in while loop this should be the condition-> zeroCount>=k

  • @vishalsrivastavavishalsri92
    @vishalsrivastavavishalsri92 Před 9 měsíci

    For this case, the code is going to fail. 111000.

  • @VinodMohananChelseaFC
    @VinodMohananChelseaFC Před 3 lety

    If the input is [1,0,0], I think this solution will give 2. But the expected result would be 1?

    • @ProgrammingTutorials1M
      @ProgrammingTutorials1M  Před 3 lety

      One flip is allowed so answer would be 2.

    • @VinodMohananChelseaFC
      @VinodMohananChelseaFC Před 3 lety +1

      @@ProgrammingTutorials1M you are right, my bad. This question is about flipping and not swapping. So the answer should be 2 as you mentioned.

  • @ajayyadav-ob4gd
    @ajayyadav-ob4gd Před 4 lety

    nice

  • @Pratik-Kedar
    @Pratik-Kedar Před 3 lety +1

    class Solution {
    public:
    inline int findMaxConsecutiveOnes(vector& nums) {
    int ans=INT_MIN;
    int n=nums.size();
    int temp=0;
    if(nums.size()==1 && nums[0]==1) return 1;
    if(nums.size()==1 && nums[0]==0) return 0;
    for(int i=0;i

  • @mohdarshad9427
    @mohdarshad9427 Před 3 lety

    i did not get proper concept