Max Consecutive Ones III
Problem statement
Given a binary array nums and an integer k, return the maximum number of consecutive ones obtainable by flipping at most k zeros to ones.
Function
longestOnes(nums: int[], k: int) → intExamples
Example 1
nums = [1,1,1,0,0,0,1,1,1,1,0]k = 2return = 6Flipping two zeros creates a six-element run.
Example 2
nums = [0,0,1,1,1,0,0]k = 0return = 3Without flips, the central run has length three.
Example 3
nums = [0,0,0]k = 3return = 3All three zeros can be flipped.
Constraints
0 ≤ nums.length ≤ 100000.- Every value in
numsis0or1. 0 ≤ k ≤ nums.length.
Source note: Author-written prompt excerpt retained for source fidelity.