sliding window October 28, 2022

Max consecutive ones

Time O(n) Space O(1) Open original problem

We will count the number of consecutive ones and update the result if it is greater than the current result.

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        count, res = 0, 0

        for num in nums:
            if num == 1:
                count += 1
                res = max(res, count)
            else:
                count = 0

        return res

Time complexity: O(n)
Space complexity: O(1)