Leetcode

Leetcode 485

Max Consecutive Ones

Sailorlqh
2021-09-21
1 min

# 485. Max Consecutive Ones

Question Link is here.

This is a daily challenge problem. The last two problems of each week are always the easiest. This problem can be solved within 2 minutes including reading the question description. Sliding window is obvious the most straightforward solution. Solution is shown below.

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        left = 0
        right = 0
        ans = 0
        if len(nums) == 0:
            return 0
        while right < len(nums):
            if nums[right] == 1:
                ans = max(ans, right - left + 1)
                right += 1
            else:
                right += 1
                left = right
        return ans
        

This solution has Time complexity of O(n)O(n).