Longest Binary Subarray With at Most K Ones
Problem statement
Given a binary array nums and an integer k, return the maximum length of a contiguous subarray containing at most k ones.
Function
longestBinarySubarrayAtMostKOnes(nums: int[], k: int) → intExamples
Example 1
nums = [1,0,1,0,0,1]k = 2return = 5Either length-five window contains exactly two ones.
Example 2
nums = [1,1,1]k = 0return = 0No non-empty subarray is allowed because every element is one.
Constraints
1 <= nums.length <= 200000nums[i]is0or1.0 <= k <= nums.length