Max Consecutive Ones III
Problem statement
Given a binary array nums and an integer k, you may change at most k zeroes into ones.
Return the maximum length of a contiguous subarray containing only ones after the 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 two zeroes gives a run of six ones.
Example 2
nums = [0,0,1,1,0,0,0,0]k = 0return = 2Without changes, the longest existing run has length two.
Constraints
1 <= nums.length <= 100000.- Each value is
0or1. 0 <= k <= nums.length.
Source note: Author-written prompt excerpt retained for source fidelity.