Problem statement

https://leetcode.com/problems/max-consecutive-ones/

Solution

Just iterate over array and keep counting ones, until we reached zero, then set it to zero. We can look at this as sliding window approach also.

Complexity

Time complexity is O(n), space is O(1).

Code

class Solution:
    def findMaxConsecutiveOnes(self, nums):
        cur_max, gl_max = 0, 0
        for i in nums:
            if i == 1:
                cur_max += 1
                gl_max = max(gl_max, cur_max)
            else:
                cur_max = 0    
        return gl_max