Max Consecutive Ones III
MediumThis is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.
Question
You get a list of 0s and 1s, plus a number k. You can flip up to k of the 0s into 1s.
After your flips, find the length of the longest run of 1s in a row you can make anywhere in the list.
Input: nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], k = 2
Output: 6
Flip the last two 0s in the middle. That turns the list into [1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0], which has a run of six 1s.
Input: nums = [0, 0, 1, 1, 1, 0, 0], k = 0
Output: 3
You can't flip anything. So the answer is the longest run that's already there, which is the three 1s in the middle.
Input: nums = [1, 0, 1, 0, 1, 0, 1], k = 2
Output: 5
Flip the first two 0s to get [1, 1, 1, 1, 1, 0, 1]. That gives you a run of five 1s.
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Remember to understand the problem and plan your approach before you start coding.