FastPrepFastPrep
Problem Brief

Max Consecutive Ones III (for E5 :)

FULLTIMEPHONE SCREEN

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 Description

Complete the function longestOnes in the editor.

longestOnes has the following parameters:

  1. 1. int[] nums: a binary array
  2. 2. int k: the maximum number of 0's that can be flipped

Returns

int: the maximum number of consecutive 1's

1Example 1

Input
nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2
Output
6
Explanation

[1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.

2Example 2

Input
nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3
Output
10
Explanation

[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

Limits and guarantees your solution can rely on.

  • 1 <= nums.length <= 10^5
  • nums[i] is either 0 or 1.
  • 0 <= k <= nums.length
public int longestOnes(int[] nums, int k) {
  // write your code here
}
Input

nums

[1,1,1,0,0,0,1,1,1,1,0]

k

2

Output

6

Sign in to submit your solution.