Given a binary array nums
, return the maximum number of consecutive 1
's in the array if you can flip at most one 0
.
Example 1:
Input: nums = [1,0,1,1,0] Output: 4 Explanation: Flip the first zero will result in 1, 1, 1, 1, 0. The maximum number of consecutive 1s is 4.
Example 2:
Input: nums = [1,0,1,1,0,1,1] Output: 4 Explanation: Flip the first zero will result in 1, 1, 1, 1, 0, 1, 1. The maximum number of consecutive 1s is 4.
Constraints:
1 <= nums.length <= 105
nums[i]
is either 0
or 1
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the maximum consecutive ones with one flip allowed means trying every possible flip. We'll go through each position, flip it, and see how many consecutive ones we get. Then, we'll pick the arrangement that gives us the most consecutive ones.
Here's how the algorithm would work step-by-step:
def max_consecutive_ones_ii_brute_force(numbers):
maximum_ones = 0
for index in range(len(numbers)):
# Try flipping each zero to one and count consecutive ones
if numbers[index] == 0:
temporary_numbers = numbers[:]
temporary_numbers[index] = 1
current_ones = 0
count = 0
for number_index in range(len(temporary_numbers)):
if temporary_numbers[number_index] == 1:
count += 1
current_ones = max(current_ones, count)
else:
# Reset count if we find a zero
count = 0
maximum_ones = max(maximum_ones, current_ones)
# If array has all ones, return the array size
if 0 not in numbers:
return len(numbers)
return maximum_ones
The best way to solve this is to use a 'window' that expands and contracts as we go through the list. The key is to keep track of how many times we've 'flipped' a zero to a one within that window, ensuring we only flip one.
Here's how the algorithm would work step-by-step:
def max_consecutive_ones_2(numbers):
maximum_consecutive_ones = 0
left_pointer = 0
zero_count = 0
for right_pointer in range(len(numbers)):
if numbers[right_pointer] == 0:
zero_count += 1
# Shrink the window if more than one zero is found
while zero_count > 1:
if numbers[left_pointer] == 0:
# Reduce zero count as it exits window.
zero_count -= 1
left_pointer += 1
# Update the max length of consecutive ones
maximum_consecutive_ones = max(
maximum_consecutive_ones, right_pointer - left_pointer + 1
)
return maximum_consecutive_ones
Case | How to Handle |
---|---|
Null or empty input array | Return 0 immediately as there are no ones. |
Array with all zeros | Return 1 since we can flip one zero. |
Array with all ones | Return the length of the array, as no flip is needed but allowed. |
Array with a single zero | Return the length of the array since a single flip yields all ones. |
Array with many consecutive zeros | The sliding window approach should efficiently handle this without integer overflow. |
Input array contains non-binary values (not 0 or 1) | Treat non-binary values as zeros. |
Integer overflow when calculating window size | The sliding window algorithm naturally avoids integer overflow by tracking indices. |
Maximum size array with alternating 0s and 1s | The sliding window should still find the optimal solution within acceptable time complexity. |