Maximum Consecutive Ones in a Circular Array
Problem statement
Given a circular binary array nums, you may change at most k zeros to ones. Return the maximum number of consecutive ones on the circle after those changes.
A circular segment may wrap from the end to the beginning, but it may contain each array position at most once, so its length never exceeds nums.length.
Function
longestCircularOnes(nums: int[], k: int) → intExamples
Example 1
nums = [1,0,1,1,0]k = 1return = 4Flipping the final zero joins the suffix and prefix for four consecutive ones.
Example 2
nums = [0,0,1]k = 1return = 2One zero can be flipped, producing a circular run of two but not three.
Constraints
1 <= nums.length <= 200000- Each value is
0or1. 0 <= k <= nums.length