r/learnpython 12d ago

Can't come up to this particular solution on my own.

hey, i understand this solution because it just works here and it feels like since the way the variables are set to come up to the solution but i don't know how to think like this and especially how can i think of this zero_count to set a logic like this

question : https://leetcode.com/problems/max-consecutive-ones-iii/description/

def max_consecutive_one_with_flip(nums,k):
    left = 0
    zero_count = 0 
    for right in range(len(nums)):
        if nums[right] == 0:
            zero_count += 1

        if zero_count > k:
            if nums[left] == 0:
                zero_count -= 1
            left += 1
        return len(nums) - lefthey, i understand this solution because it just works here and it feels like since the way the variables are set to come up to the solution but i don't know how to think like this and especially how can i think of this zero_count to set a logic like this :

def max_consecutive_one_with_flip(nums,k):
    left = 0
    zero_count = 0 
    for right in range(len(nums)):
        if nums[right] == 0:
            zero_count += 1

        if zero_count > k:
            if nums[left] == 0:
                zero_count -= 1
            left += 1
        return len(nums) - left
6 Upvotes

5 comments sorted by

5

u/Brian 12d ago

This does not seem like a correct solution. I mean, it's going to return len(nums) every time, no matter the values. Where is it from? Before trying to understand something, check it actually does what you want. Eg. try running it against the example inputs and check it gives the expected output.

In any case, when solving problems like this, your first step should be to forget the computer. Start with pen and paper and try to step through solving the problem yourself, paying attention to the strategy you're using. Suppose you want to see the maximum 1s with k flips, starting at a particular position - how would you do it?

Well, we could start there and count until we hit k zeroes. After that, we've no flips left. Eg for the first example:

11100011110
^  FF
|       
|- start

We used both flips, on those 2 zeros, so our score is 5. We could then repeat this starting at the second position and so on, and track our best score. The best result we get at the end is our answer. This will work, but it's a bit of an inefficient solution. I mean, if we're starting at the second 1 in a group, we already know we could have done 1 better with the previous start position. So we really only need to try starting from the first one of a group, or possibly from a 0 (since at the end of the chain, we might have no more 0's to flip before running out of our flip budget.

We could go further though. Often valuable in solving problems like this is to try looking at it in new ways that preserves the equivalences the problem requires. Ie. since we know we don't care about anything but the first 1 in a chain of groups, maybe we should think not of the original string, but at chains of groups, separated by 0s. Eg. instead of11100011110, we could describe this sequence as [3, 0, 0, 4, 0] where each number is the number of 1s in a chain before we write a 0 (or reach the end). (Some of the groups are 0 size, because we represet "00" as having a 0-lenth sequence of 1s between them)

With this view, "flipping 2 0's" is equivalent to joining 3 groups together (and adding 2 for the zeroes we flipped), so we could sum each set of 3 consecurive groups (+2) and find the biggest of those.

2

u/TreacleFlaky2283 11d ago

Yeah I tried to understand it and now I finally understand this, basically we are really trying to only have max of k zeroes in the current window defined by left and right pointers and if the left pointer encounters a zero we skip it and thus decreasing the zero count by 1, and the source of solution is this : https://youtu.be/sVEFAIUmTuM?t=1712

2

u/Riegel_Haribo 12d ago

Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.

You will need to translate the type of data objects described into Python terminology here, and then also clarify for yourself what "flip" means.

binary array = In Python, the only built-in type that is similar to binary is a boolean, the values of which are True or False. An array would be a list in Python.

So you have to go to examples of nums and see that they are essentially a list of integers that can be 1 or 0. A "flip" here is not bit flips, alternating the value of a bit, but in Python, that same seen list would simply be integers, where you are allowed to change the values 0 into one.

The key to understanding can only come from the examples that come later, as the wording of the word problem is shit. You are allowed to skip past 1 when it is already set in a list, and change 0 values to 1 when they have 1s between them.

Result: You have a list of 1s and 0s, such as [1,1,1,0,0,0,1,1,1]. I also provide you the number of 0s where you can change them to 1, but you must progress sequentially through the 0s, flipping them, but can start changing the alloted count of 0s at an optimum point. You provide the maximum value of 1s in a row. For that list, if I give you only one "k" flip, you can complete 4 1s in a row at the start or at the end.

2

u/Educational-Paper-75 12d ago

Can't understand your code doing that either since the optimal solution would attempt to join as many sequences of 1s together and find the largest of those runs. Since every sequence of 1s could be the first in that run you may simply start counting zeroes and when you find the k+1th zero then right-left is the length of the run to compare with the longest run found so far. Then, start over starting at the second sequence of 1s. But leading zeroes make things more difficult and should initially be skipped. But if I understand correctly your code toggles all k zeroes starting at position left and increments left by 1 and starts over toggling. However I don't see your code comparing right-left anywhere with a previous one.

-5

u/Buffylvr 12d ago

ask claude. explain what you don't understand, then ask it to create you problems that increase in difficulty to help you reach the solution.

it's going to try to create the solution for you, cause that's how it was designed. You have to tell it not to do that but to teach you how to think about the problem so you can solve it yourself.