Maximum Consecutive Ones With a Single Bit Repair
Arrays & HashingAuto-graded · 22 test casesLast updated
Find the longest stretch of 1s achievable in a binary list when at most one 0 may be changed to 1, using a two-pointer window that tolerates one zero.
Maximum Consecutive Ones With a Single Bit Repair
You are given a list bits that contains only 0 and 1. You may change at most one 0 into a 1 (you may also choose to change nothing). After this optional change, look at the longest block of consecutive 1s in the list.
Return the largest block length that can be reached by choosing the change optimally.
Rules
- Changing nothing is allowed, so a list that has no zeros simply returns its own length.
- If every element is
0, the best you can do is change one of them, so the answer is1. - If the list is empty, return
0. - Only the length is returned, not the position of the change.
Constraints
1 <= len(bits) <= 10^5- Each element of
bitsis0or1. - An empty list should still return
0, although the size bound above normally excludes it.
Examples
Example 1
Input: max_ones_with_one_repair([1, 0, 1, 1, 0])
Output: 4
Example 2
Input: max_ones_with_one_repair([0, 0, 0])
Output: 1
Hints
Hint 1
Think of the answer as the longest contiguous segment that contains at most one zero; that zero is the one you would change.
Hint 2
Maintain a window with two indices and a count of zeros inside it. Grow the right edge one step at a time and record the window length after each step.
Hint 3
When the zero count reaches two, advance the left edge until a zero leaves the window. Each index enters and leaves at most once, so the total work is linear.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Changing at most one 0 to 1 and then taking the longest block of 1s is the same as finding the longest contiguous segment containing at most one zero. If the segment has a zero, that is the one we change; if it has none, we change nothing.
Use a sliding window [left, right] and a counter zeros for the zeros inside it:
- Move
rightacross the list. Ifbits[right]is0, incrementzeros. - While
zeros > 1, moveleftforward. When the element leaving the window is a0, decrementzeros. - The window now is the longest valid window ending at
right. Update the best length withright - left + 1.
Why it is correct
- After the shrink step the window holds at most one zero, so every length we record is actually achievable.
leftonly advances while the window has two zeros, so it sits at the smallest valid start for the currentright. Any longer window ending here would contain at least two zeros.- Extending
rightcan never reduce the zero count, so the smallest validleftnever moves backward. That is why a single forward pass of two pointers is enough. - The optimal segment ends at some index, and when
rightreaches it, the window we measure is at least as long as the optimal one.
Edge cases fall out naturally: no zeros gives the full length, all zeros gives 1 (each new zero forces the window down to one element), and an empty list never enters the loop so 0 is returned.
Complexity
- Time: O(n). Both pointers move forward at most n steps in total.
- Space: O(1).
Reference solution
from typing import List
def max_ones_with_one_repair(bits: List[int]) -> int:
left = 0
zeros = 0
best = 0
for right, value in enumerate(bits):
if value == 0:
zeros += 1
while zeros > 1:
if bits[left] == 0:
zeros -= 1
left += 1
best = max(best, right - left + 1)
return best