1. Description
Given a binary array nums, return the maximum number of consecutive 1's in the array.
constraints :
- 1 <= nums.length <= \(10^{5}\)
- nums[i] is either 0 or 1.
2. Algorithms
- Start
- Declare three variables count, i, and max_num. Initialize them with zero.
- Repeat the steps until i == len(nums).
- If nums[i] equals to zero, update max_num with max value between count and max_num. After that, initialize count to zero.
- Increment count and zero by 1.
- Return the max between final count and max_num.
- End
3. Codes
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
# i : Index for iterating through nums.
# count : Count of consecutive 1's in the array.
# max_num : Storing max count of consecutive 1's
i, count, max_num = 0, 0, 0
while i < len(nums) :
if nums[i] == 0 :
max_num = max(count, max_num)
count = -1
count += 1
i += 1
return max(count, max_num)
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
495 Teemo Attacking (0) | 2022.09.01 |
---|---|
492 Construct the Rectangle (0) | 2022.08.31 |
482 License Key Formatting (0) | 2022.08.31 |
476 Number Complement (0) | 2022.08.31 |
463 Island Perimeter (0) | 2022.08.31 |