-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0485_max_consecutive_ones.py
More file actions
40 lines (32 loc) · 977 Bytes
/
0485_max_consecutive_ones.py
File metadata and controls
40 lines (32 loc) · 977 Bytes
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
32
33
34
35
36
37
38
39
40
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = 0
maxOnes = 0
for num in nums:
# Count consecutive ones
if (num == 1):
count += 1
# If 0 occurs
else:
maxOnes = max(count, maxOnes)
count = 0
return max(count, maxOnes)
"""
# Use index of 0
maximum = 0
while (0 in nums):
# Find the index of 0
numIndex = nums.index(0)
# Update maximum
maximum = max(numIndex, maximum)
# Update nums
if (numIndex == len(nums)):
return maximum
else:
nums = nums[numIndex + 1:]
return max(len(nums), maximum)
"""