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.
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
nums = [1,1,1,0,0,0,1,1,1,1,0] k = 2 return = 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.
nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1] k = 3 return = 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.
1 <= nums.length <= 10^5nums[i]is either 0 or 1.0 <= k <= nums.length
- Merge Three Sorted ArraysPHONE SCREEN · Seen May 2026
- Diagonal Traverse (for E4 ;)PHONE SCREEN · Seen Mar 2025
- Find Peak ElementPHONE SCREEN · Seen Mar 2025
- Find Pair Closest to K (for E5 :)PHONE SCREEN · Seen Feb 2025
- Get Minimum Round Trip Cost (: for E4 && E5 :)PHONE SCREEN · Seen Feb 2025
- Nested List Weight Sum (for E4)PHONE SCREEN · Seen Feb 2025
- Sliding Window Average (Meta Canada, E4/E5 :)PHONE SCREEN · Seen Dec 2024
- Build Blocks and Obstacles on a Number LineSeen Oct 2024
public int longestOnes(int[] nums, int k) {
// write your code here
}