1004.Max Consecutive Ones III
Problem description:
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:
1 | Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 |
Example 2:
1 | Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 |
Constraints:
1 <= nums.length <= 105
nums[i]
is either0
or1
.0 <= k <= nums.length
Solution:
Sliding window to find the range that could contain at most k
0
Count the number of 1 and add the current_zeros in the window
1 | class Solution: |
time complexity: $O()$
space complexity: $O()$
reference:
related problem: