-
Notifications
You must be signed in to change notification settings - Fork 0
/
findMaxConsecutiveOnes.py
31 lines (26 loc) · 1.3 KB
/
findMaxConsecutiveOnes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Max Consecutive Ones
# Given a binary array, find the maximum number of consecutive 1s in this array.
# Example 1:
# Input: [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.
# Note:
# The input array will only contain 0 and 1.
# The length of input array is a positive integer and will not exceed 10,000
# Hide Hint #1
# You need to think about two things as far as any window is concerned. One is the starting point for the window. How do you detect that a new window of 1s has started? The next part is detecting the ending point for this window. How do you detect the ending point for an existing window? If you figure these two things out, you will be able to detect the windows of consecutive ones. All that remains afterward is to find the longest such window and return the size.
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxones=0
currones=0
for el in nums:
if el == 0:
if currones > maxones:
maxones = currones
currones = 0
else:
currones +=1
if currones > maxones:
maxones = currones
return maxones