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 <= 10^5
nums[i]
is either 0
or 1
.0 <= k <= nums.length
def longestOnes(nums, k):
left = 0
zeros = 0
max_length = 0
for right in range(len(nums)):
if nums[right] == 0:
zeros += 1
while zeros > k:
if nums[left] == 0:
zeros -= 1
left += 1
max_length = max(max_length, right - left + 1)
return max_length
The function longestOnes
takes a binary array nums
and an integer k
as input. It returns the maximum number of consecutive 1s in the array if you can flip at most k
0s.
The algorithm uses a sliding window approach. The left
pointer represents the left boundary of the window, and the right
pointer represents the right boundary of the window. The zeros
variable keeps track of the number of 0s in the current window.
For each element in the array, we move the right
pointer to the right. If the current element is 0, we increment the zeros
counter. If the number of 0s in the window exceeds k
, we move the left
pointer to the right until the number of 0s in the window is less than or equal to k
. During this process, if the element at the left
pointer is 0, we decrement the zeros
counter.
At each step, we update the max_length
variable to be the maximum of the current max_length
and the length of the current window (right - left + 1
).
Finally, we return the max_length
.
nums = [1,1,1,0,0,0,1,1,1,1,0]
k = 2
print(longestOnes(nums, k)) # Output: 6
nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1]
k = 3
print(longestOnes(nums, k)) # Output: 10
A brute force solution would be to iterate through all possible subarrays and, for each subarray, count the number of 0s. If the number of 0s is less than or equal to k
, we update the max_length
variable.
def longestOnes_brute_force(nums, k):
max_length = 0
for i in range(len(nums)):
for j in range(i, len(nums)):
subarray = nums[i:j+1]
zeros = subarray.count(0)
if zeros <= k:
max_length = max(max_length, len(subarray))
return max_length
The time complexity of the sliding window solution is O(n), where n is the length of the input array. This is because we iterate through the array once with the right
pointer, and the left
pointer can only move from left to right once in the array. Thus, both left
and right
pointers each visit each element a single time.
The time complexity of the brute force solution is O(n^3), where n is the length of the input array. This is because we iterate through all possible subarrays (O(n^2)), and for each subarray, we count the number of 0s (O(n)).
The space complexity of the sliding window solution is O(1), because we only use a constant amount of extra space for the left
, right
, zeros
, and max_length
variables.
The space complexity of the brute force solution is also O(1), because we only use a constant amount of extra space for the max_length
variable. The subarray
does create a slice of nums
, but in terms of asymptotic complexity, it is bounded by O(n), but is dominated by the O(n^3) time complexity.