최대 1 분 소요

Question

Given a binary array nums, return the maximum number of consecutive 1’s in the array.

0과 1로 이루어진 리스트가 주어질 때, 연속된 1의 개수의 최대를 구하는 문제였다.

Example

Input: nums = [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.
Input: nums = [1,0,1,1,0,1]
Output: 2

Constraints

  • 1 <= nums.length <= 105
  • nums[i] is either 0 or 1.

Solution 1

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        
        cnt=0
        max=0
        
        for x in nums:
            if x==1:
                cnt+=1
                if max<cnt:
                    max=cnt
            else:
                cnt=0
                
        return max

끄적

  • if 조건문을 이용해 간단하게 풀 수 있었다.

Ref.

새우버거의 개발 블로그



💛 개인 공부 기록용 블로그입니다. 👻

맨 위로 이동하기