Notice
Recent Posts
Recent Comments
Link
오늘도 개발
0. Max Consecutive Ones 본문
문제
이진수로 이루어진 nums 배열을 인수로 받는다.
이 중 가장 긴 1의 반복 횟수를 구하시오.
Input: nums = [1,1,0,1,1,1]
Output: 3
내가 생각한 방식
최대 반복된 횟수를 알 수 있도록 max 변수 만들어서 비교, 반복된 횟수가 가장 많은 경우 max에 저장
// c++
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int count = 0;
int max = 0;
for (int n : nums){
if (n==1){
count++;
if (max < count) max = count;
}
else count = 0;
}
return max;
}
};
# python
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count = 0
maximum = 0
for num in nums:
if num == 1:
count += 1
maximum = max(maximum, count)
else:
count = 0
return maximum
'자료구조 & 알고리즘 > Leetcode' 카테고리의 다른 글
| 2. duplicate Zeros (0) | 2022.05.14 |
|---|---|
| 1. Find Numbers with Even Number of Digits (0) | 2022.05.06 |
| 파이썬 코딩 도장 - Palindrome 검사하기 (0) | 2022.04.13 |
| 파이썬 코딩 도장 - 지뢰 찾기(23.7) (0) | 2022.04.10 |
| 파이썬 코딩 도장 - FizzBuzz (0) | 2022.04.08 |