Skip to contentKEEP THE CRAFTTHE SANCTUARY

Longest Ones After Deleting One Element

Delete exactly one element from a binary array and find the longest run of 1s that is left.

The problem

Implement Solution.longestSubarray(nums). nums contains only 0s and 1s. You must delete exactly one element. Return the length of the longest non-empty run of consecutive 1s in the array after the deletion, or 0 if no 1s remain. For example, [1, 1, 0, 1] gives 3 (delete the 0), and [1, 1, 1] gives 2 (you still have to delete one 1). nums has between 1 and 100,000 elements.

Starter code

class Solution:
    def longestSubarray(self, nums):
        pass

Public examples

  1. nums = [1, 1, 0, 1]gives3
  2. nums = [0]gives0
  3. nums = [1, 1, 1, 1]gives3

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

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