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.
Example 1:
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2:
Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Constraints:
1 <= nums.length <= 105nums[i] is either 0 or 1.0 <= k <= nums.lengthWhen 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:
Imagine you have a long row of alternating light and dark tiles, and you are allowed to flip a few dark tiles to light ones to make the longest possible continuous stretch of light tiles. The brute force approach involves testing every single possible starting point and ending point in the row, and counting how many dark tiles would need to be flipped in between.
Here's how the algorithm would work step-by-step:
def longest_ones(binary_array, maximum_flips):
maximum_length = 0
# Iterate through every possible starting position to test all subarrays
for start_index in range(len(binary_array)):
flipped_count = 0
for end_index in range(start_index, len(binary_array)):
if binary_array[end_index] == 0:
flipped_count += 1
# Stop expanding this subarray if we exceed our allowed flips
if flipped_count > maximum_flips:
break
current_length = (end_index - start_index) + 1
# Keep track of the longest valid stretch we encounter
if current_length > maximum_length:
maximum_length = current_length
return maximum_lengthInstead of checking every single possible stretch of zeros and ones, we can use a clever sliding window strategy. This approach expands our view to capture as many matching items as possible, and smoothly slides forward whenever we run out of our allowed swaps.
Here's how the algorithm would work step-by-step:
def longest_ones(binary_array, allowed_flips):
window_start = 0
zero_count = 0
max_sequence_len = 0
for window_end in range(len(binary_array)):
if binary_array[window_end] == 0:
zero_count += 1
# We shrink the window from the left when zero count exceeds our allowed limit.
if zero_count > allowed_flips:
if binary_array[window_start] == 0:
zero_count -= 1
window_start += 1
# Track the maximum valid window size found so far during our scan.
current_window_size = window_end - window_start + 1
if current_window_size > max_sequence_len:
max_sequence_len = current_window_size
return max_sequence_len| Case | How to Handle |
|---|---|
| Empty input array where nums is null or has length zero | The sliding window loop will not execute, returning the correct max length of zero. |
| Array size of one with k equals zero or one | The pointer logic correctly evaluates the single element and returns its value. |
| All zeros in the array and k equals zero | The window will collapse to a size of zero and return the correct maximum length of zero. |
| All zeros in the array and k is greater than or equal to the array length | The right pointer will traverse the entire array and return the total array length as the result. |
| All ones in the array regardless of the value of k | No zeros need to be flipped, and the sliding window expands to encompass the entire array returning its length. |
| k is greater than the total number of zeros in the array | The window expands to the end of the array without violating the zero count constraint, returning the full array length. |
| Maximum possible array length with up to one hundred thousand elements | The linear time sliding window approach processes the array efficiently in O(n) time avoiding time limit errors. |
| Alternating ones and zeros with a small k value | The left pointer contracts efficiently whenever the zero count exceeds k maintaining an accurate maximum window size. |