Problems
Given a binary array nums, return the maximum number of consecutive 1's in the array.
Example 1:
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.
Constraints:
- 1 <= nums.length <= 105
- nums[i] is either 0 or 1.
Solution
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
res, cnt = 0, 0
for i in range(len(nums)):
if nums[i] == 1:
cnt += 1
else:
res = max(res, cnt)
cnt = 0
return max(res, cnt)
'알고리즘 > 기타 알고리즘' 카테고리의 다른 글
[LeetCode] Squares of a Sorted Array (Python) (0) | 2022.05.16 |
---|---|
[LeetCode] Find Numbers with Even Number of Digits (Python) (0) | 2022.05.16 |
[이것이 코딩 테스트다 with Python] 고정점 찾기 : 이진탐색 (0) | 2022.01.24 |
[완전 탐색, 시뮬레이션] 시각 (0) | 2022.01.24 |
[완전 탐색] 상하 좌우 (0) | 2022.01.24 |