Problem · Array
Max Consecutive Ones III
Learn this problemProblem statement
Given a binary array nums and an integer k, you may change at most k zeroes into ones.
Return the maximum number of consecutive ones obtainable after those changes.
Function
longestOnes(nums: int[], k: int) → intExamples
Example 1
nums = [1,1,1,0,0,0,1,1,1,1,0]k = 2return = 6Changing the two zeroes between the first and last groups gives a longest run of six ones.
Example 2
nums = [0,0,1,1,1,0,0]k = 0return = 3No changes are allowed, so the existing run of three ones is optimal.
Constraints
1 <= nums.length <= 10^5nums[i]is0or1.0 <= k <= nums.length