Leetcode # 485. Max Consecutive Ones

Problem

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

Solution

Time Complexity: O(len(nums))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
    ans = curr = 0
    for i in range(len(nums)):
      if nums[i] == 1: curr += 1
      else:
        ans, curr = max(ans, curr), 0
    
    return max(ans, curr)

 

Last Updated on 2023/08/29 by A1go

目錄
Bitnami