Practice/Meta/Leetcode 1004. Max Consecutive Ones III
CodingMust
You are given a binary array nums (containing only 0s and 1s) and an integer k. You can flip at most k zeros to ones. Your goal is to find the maximum number of consecutive 1s in the array after performing these flips optimally.
k zerosExample 1:
Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 Output: 6 Explanation: By flipping the zeros at indices 4 and 5, we get: [1,1,1,0,0,1,1,1,1,1,1] The bold sequence [0,1,1,1,1,1,1] has length 6 (after flipping index 4).
Example 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: Flip the zeros at indices 5, 9, and 12 to maximize consecutive ones.
Example 3:
Input: nums = [1,1,1,1], k = 0 Output: 4 Explanation: No flips needed; the array already has 4 consecutive ones.