Skip to contentKEEP THE CRAFTTHE SANCTUARY

Max Consecutive Ones III

Find the longest window of 0s and 1s that holds at most k zeros.

The problem

Implement Solution.longestOnes(nums, k). nums contains only 0s and 1s, and you may flip at most k zeros into ones. Return the length of the longest contiguous subarray that can be all ones after the flips. For example, nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0] and k = 2 returns 6. 1 <= len(nums) <= 100000 and 0 <= k <= len(nums).

Starter code

class Solution:
    def longestOnes(self, nums, k):
        pass

Public examples

  1. nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], k = 2gives6
  2. nums = [1, 1, 1, 0, 0], k = 0gives3
  3. nums = [0, 0, 0, 0], k = 2gives2

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.