Max consecutive ones
October 28, 2022
sliding-windowProblem URL: Max consecutive ones
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)