//GitHub https://github.com/vellimole0621
Max Consecutive Ones
https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3238/
문제 설명
:
Given a binary array nums, return the maximum number of consecutive 1's in the array.
=> 주어진 배열에서, '1'이 최대 몇번 연속되는지 구하라.
내 풀이
:
최대 수 담을 변수, 중간 변수 생성 > 배열 내 반복문을 돌려, 1인 경우 중간 변수를 늘리고 0인 경우 중간 변수를 0으로 초기화 시킨다. 또한 중간 변수가 최대 수 보다 커지는 경우 최대 수 변수값을 중간 변수로 바꾼다. > 최대 수 변수값을 반환
# Max Consecutive Ones
# https://leetcode.com/explore/learn/card/fun-with-arrays/521/introduction/3238/
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
max_one = 0
temp = 0
for i in range(0, len(nums)):
if nums[i] == 1:
temp += 1
elif nums[i] == 0:
temp = 0
if max_one < temp:
max_one = temp
return max_one
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Squares of a Sorted Array (0) | 2023.09.05 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Find Numbers with Even Number of Digits (0) | 2023.08.29 |
[파이썬 Python/알고리즘] LeetCode - Ransom Note (0) | 2023.08.01 |
[파이썬 Python/알고리즘] LeetCode - Number of Steps to Reduce a Number to Zero (0) | 2023.07.31 |
[파이썬 Python/알고리즘] LeetCode - Fizz Buzz (0) | 2023.07.31 |