487. Max Consecutive Ones II
https://leetcode.com/problems/max-consecutive-ones-ii
Python
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
length = len(nums)
if length < 2:
return 1
if 0 not in nums:
return length
target_index = None
max_ones = 0
for i in range(0, length):
if nums[i] == 0:
counter = 0
for j in range(i-1, -1, -1):
if nums[j] == 0:
break
counter += 1
for j in range(i+1, length):
if nums[j] == 0:
break
counter += 1
if counter > max_ones:
target_index = i
max_ones = counter
return max_ones + 1