🔍 문제
Given a binary array nums, return the maximum number of consecutive 1's in the array if you can flip at most one 0.
nput: nums = [1,0,1,1,0]
Output: 4
Explanation: Flip the first zero will get the maximum number of consecutive 1s.
After flipping, the maximum number of consecutive 1s is 4.
🖥 실행
1) Answer
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
# previous and current length of consecutive 1
pre, curr, maxlen = -1, 0, 0
for n in nums:
if n == 0:
pre, curr = curr, 0
else:
curr += 1
maxlen = max(maxlen, pre + 1 + curr )
return maxlen
'Coding test > LeetCode' 카테고리의 다른 글
[LeetCode] Find All Numbers Disappeared in an Array (0) | 2022.05.23 |
---|---|
[LeetCode] Third Maximum Number (0) | 2022.05.20 |
[LeetCode] Height Checker (0) | 2022.05.20 |
[LeetCode] Move Zeroes (0) | 2022.05.20 |
[LeetCode] Replace Elements with Greatest Element on Right Side (0) | 2022.05.20 |