Skip to content

Commit bc0b23d

Browse files
committed
feat(puzzles, arrays): max consecutive ones
1 parent d3045ee commit bc0b23d

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

puzzles/arrays/max_consecutive_ones/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ Explanation: [0,0,1,1,*1*,*1*,1,1,1,*1*,1,1,0,0,0,1,1,1,1]
1919
starred numbers were flipped from 0 to 1. The longest subarray is [1,1,1,1,1,1,1,1,1,1].
2020
```
2121

22+
## Max consecutive ones two
23+
24+
You are given a binary array nums (an array that contains only 0s and 1s). Your task is to find the maximum number of
25+
consecutive 1s in the array and return it.
26+
27+
### Constraints
28+
29+
- 1 ≤ `nums.length` ≤ 10^3
30+
- `nums[i` is either 0 or 1
31+
2232
## Related Topics
2333

2434
- Array

puzzles/arrays/max_consecutive_ones/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,36 @@ def longest_ones(nums: List[int], k: int) -> int:
2020
left += 1
2121

2222
return right - left + 1
23+
24+
def find_max_consecutive_ones(nums: List[int]) -> int:
25+
"""
26+
Finds the maximum consecutive ones in a binary array and returns it.
27+
28+
The most straightforward way to solve this is to use a single pass through the array, keeping track of two values
29+
as we go. First, we need a counter for the current streak of consecutive 1s we're seeing. Second, we need to
30+
remember the maximum streak we've encountered so far.
31+
32+
As we examine each element, if we see a 1, we increment our current streak counter. If we see a 0, that breaks our
33+
streak, so we reset the counter to 0. Importantly, every time we update our current streak, we check whether it's
34+
larger than our maximum and update the maximum if needed.
35+
36+
Time complexity is O(n) where n is the length of the input array
37+
Space complexity is O(1) as no extra space is required
38+
39+
Args:
40+
nums(list): a list of 1s and 0s.
41+
Returns:
42+
int: maximum number of consecutive 1s in the nums binary array
43+
"""
44+
if len(nums) == 0:
45+
return 0
46+
max_ones = 0
47+
current_consecutive = 0
48+
49+
for num in nums:
50+
if num == 1:
51+
current_consecutive += 1
52+
max_ones = max(max_ones, current_consecutive)
53+
else:
54+
current_consecutive = 0
55+
return max_ones

puzzles/arrays/max_consecutive_ones/test_max_consecutive_ones.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import unittest
2-
3-
from . import longest_ones
2+
from typing import List
3+
from parameterized import parameterized
4+
from puzzles.arrays.max_consecutive_ones import longest_ones, find_max_consecutive_ones
45

56

67
class MaxConsecutiveOnesTestCase(unittest.TestCase):
7-
def test_one(self):
8-
"""should return 6 from nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2"""
9-
nums = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0]
10-
k = 2
11-
expected = 6
8+
9+
@parameterized.expand([
10+
([1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 2, 6),
11+
([0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], 3, 10),
12+
])
13+
def test_longest_ones(self, nums: List[int], k: int, expected: int):
1214
actual = longest_ones(nums, k)
1315
self.assertEqual(expected, actual)
1416

15-
def test_two(self):
16-
"""should return 10 from nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3"""
17-
nums = [0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1]
18-
k = 3
19-
expected = 10
20-
actual = longest_ones(nums, k)
17+
@parameterized.expand([
18+
([1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0], 4),
19+
([0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1], 4),
20+
([1,1,0,0,1,1,1,0,0,1,0], 3),
21+
([1,1,0,0,1,1], 2),
22+
([1,1,1,0,1,1,1,1], 4),
23+
([0,0,0,0], 0),
24+
])
25+
def test_find_max_consecutive_ones(self, nums: List[int], expected: int):
26+
actual = find_max_consecutive_ones(nums)
2127
self.assertEqual(expected, actual)
2228

2329

0 commit comments

Comments
 (0)