python/CodeChallenge_30.py

34 lines
715 B
Python

'''
Given a binary array consisting of only 0s and 1s, return the length of the longest segment of consecutive 0s (also referred to as a blank space) in the array.
Example 1
Input:
[1, 0, 0, 1, 0]
Output:
2
Example 2
Input:
[1, 1, 1]
Output:
0
'''
from typing import List
class Solution(object):
def solve(self, a:List[int]) -> int:
max_length = 0
current_length = 0
for num in a:
if num == 0:
current_length += 1
max_length = max(max_length, current_length)
else:
current_length = 0
return max_length
s = Solution()
print(s.solve([1, 0, 0, 1, 0]))
# Output: 2
print(s.solve([1, 1, 1]))
# Output: 0