본문 바로가기
LeetCode/Arrays 101

Leetcode. Max Consecutive Ones

by 벤진[Benzene] 2020. 6. 29.

Problem:

Given a binary array, find the maximum number of consecutive 1s in this array.

 

Example 1:

Input: [1,1,0,1,1,1]

Output: 3

Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of consecutive 1s is 3.

 

Note:

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000
class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        
        res = 0
        count = 0
        
        for i in range(len(nums)):
            if nums[i] == 1:
                count += 1
            else:
                res = max(res,count)
                count = 0
       
        return max(res,count)

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

'LeetCode > Arrays 101' 카테고리의 다른 글

Leetcode. Squares of a Sorted Array  (1) 2020.07.04
Leetcode. Find Numbers with Even Number of Digits  (0) 2020.07.01

댓글