-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path485.py
More file actions
68 lines (61 loc) · 1.59 KB
/
485.py
File metadata and controls
68 lines (61 loc) · 1.59 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Method1
# class Solution(object):
# def findMaxConsecutiveOnes(self, nums):
# """
# :type nums: List[int]
# :rtype: int
# """
# max_consecutive = 0
#
# i = 0
# while i < len(nums):
# curr = 0
# while nums[i] == 1:
# curr += 1
# i += 1
# if i == len(nums):
# break
# i = i+1
# max_consecutive = max(curr, max_consecutive)
#
# return max_consecutive
# Method2
# class Solution(object):
# def findMaxConsecutiveOnes(self, nums):
# """
# :type nums: List[int]
# :rtype: int
# """
# max_consecutive = 0
# curr = 0
# for i in nums:
# if i == 1:
# curr += 1
# else:
# max_consecutive = max(max_consecutive, curr)
# curr = 0
#
# max_consecutive = max(max_consecutive, curr)
# return max_consecutive
# method3 fastest!
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_consecutive = 0
curr = 0
for i in nums:
if i == 1:
curr += 1
else:
if max_consecutive < curr:
max_consecutive = curr
curr = 0
if max_consecutive < curr:
max_consecutive = curr
return max_consecutive
nums = [1, 1, 0, 1, 1, 1]
test = Solution()
print(test.findMaxConsecutiveOnes(nums))