Max Consecutive Ones III (for E5 :)
Learn this problemProblem statement
This is Max Consecutive Ones III, LC 1004 :)
The other question was - Get Minimum Round Trip Cost (: for E4 && E5 :) --> Click me, I ma a link~
Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's.
Function
longestOnes(nums: int[], k: int) → int
Complete the function longestOnes in the editor.
longestOnes has the following parameters:
- 1.
int[] nums: a binary array - 2.
int k: the maximum number of 0's that can be flipped
Returns
int: the maximum number of consecutive 1's
Examples
Example 1
nums = [1,1,1,0,0,0,1,1,1,1,0]k = 2return = 6
[1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2
nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1]k = 3return = 10
[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Constraints
1 <= nums.length <= 10^5nums[i]is either 0 or 1.0 <= k <= nums.length