Dodomeki
Sign in

135. Longest Segment After Up to k Flips

Medium

You are given an array nums containing only 0 and 1, and an integer k. You may choose up to k positions whose value is 0 and flip them to 1.

Return the length of the longest contiguous segment that can consist entirely of 1s after these flips. You do not need to modify nums.

Example 1
Input: nums = [1, 0, 1, 1, 0], k = 1
Output: 4
Explanation: Flipping the zero at the second position makes it possible to form a segment of four consecutive ones.
Example 2
Input: nums = [0, 0, 1, 1, 1, 0], k = 2
Output: 5
Explanation: The two flips allow a segment of five consecutive ones.
Example 3
Input: nums = [1, 1, 1], k = 0
Output: 3
Constraints
  • 0 <= nums.length <= 100000
  • nums[i] is 0 or 1
  • 0 <= k <= nums.length
Sliding windowArrays
Caso 1
nums =
[1,0,1,1,0]
k =
1
Expected =
4
Caso 2
nums =
[0,0,1,1,1,0]
k =
2
Expected =
5
Caso 3
nums =
[1,1,1]
k =
0
Expected =
3

Run checks these 3 cases. Submit runs all 12, including hidden ones.