Skip to content

Commit f9d2ef5

Browse files
committed
Add Find Max Consecutive Ones (#178)
1 parent 35f8730 commit f9d2ef5

6 files changed

Lines changed: 102 additions & 34 deletions

‎README.md‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@ to prevent obscuring the function and logic of the algorithms / data-structures.
3939

4040
### Local development
4141

42-
- Install `pyenv`
43-
- Install current Python version: `pyenv install 3.12`
44-
- Set global Python version: `pyenv global 3.12`
45-
- Initialize virtual environment: `python -m venv .venv`
46-
- Activate: `source .venv/bin/activate`
47-
- Install packages: `pip3 install -r requirements.txt`
42+
* Install `pyenv`
43+
* Install current Python version: `pyenv install 3.12`
44+
* Set global Python version: `pyenv global 3.12`
45+
* Initialize virtual environment: `python -m venv .venv`
46+
* Activate: `source .venv/bin/activate`
47+
* Install packages: `pip install -r requirements.txt`
4848

4949
### VS Code Dev Container
5050

51-
- Download VSCode and Docker
52-
- Launch project inside of Docker container in VSCode
53-
- VSCode does the rest!
51+
* Download VSCode and Docker
52+
* Launch project inside of Docker container in VSCode
53+
* VSCode does the rest!
5454

5555
## Project Commands
5656

‎arrays/README.md‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [Find Disappeared Numbers](#find-disappeared-numbers)
88
* [Find Index of Largest Number](#find-index-of-largest-number)
99
* [Find Max Consecutive Ones](#find-max-consecutive-ones)
10+
* [Find Max Consecutive Ones With One Flip](#find-max-consecutive-ones-with-1-flip)
1011
* [Find Smallest Positive Integer](#find-smallest-positive-integer)
1112
* [First Duplicate](#first-duplicate)
1213
* [First Not Repeating Character](#first-not-repeating-character)
@@ -44,7 +45,7 @@ More formally check if there exists two indices i and j such that :
4445

4546
`i != j`
4647
`arr[i] == 2 * arr[j]`
47-
48+
4849
Example 1:
4950

5051
Input: `[10,2,5,3]`
@@ -198,6 +199,42 @@ def find_index_largest_number(numbers: List[int]) -> int:
198199

199200
## Find Max Consecutive Ones
200201

202+
You are given a binary array nums, return the maximum number of consecutive 1's in the array.
203+
204+
Example 1:
205+
206+
Input: nums = [1,1,0,1,1,1]
207+
208+
Output: 3
209+
210+
Example 2:
211+
212+
Input: nums = [1,0,1,1,0,1]
213+
214+
Output: 2
215+
216+
```python
217+
def find_max_consecutive_ones(nums: List[int]) -> int:
218+
max_ones = 0
219+
220+
if not nums:
221+
return max_ones
222+
223+
ones_count = 0
224+
225+
for num in nums:
226+
if num == 1:
227+
ones_count += 1
228+
else:
229+
ones_count = 0
230+
231+
max_ones = max(max_ones, ones_count)
232+
233+
return max_ones
234+
```
235+
236+
## Find Max Consecutive Ones With 1 Flip
237+
201238
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.
202239

203240
Example 1:
@@ -207,7 +244,7 @@ Output: `4`
207244
Explanation: Flip the first zero will get the the maximum number of consecutive 1s. After flipping, the maximum number of consecutive 1s is 4.
208245

209246
```python
210-
def find_max_consecutive_ones(nums: List[int]) -> int:
247+
def find_max_consecutive_ones_with_flip(nums: List[int]) -> int:
211248
zero = -1
212249
ones = 0
213250
highest = 0

‎arrays/find_max_consecutive_ones.py‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,19 @@
22

33

44
def find_max_consecutive_ones(nums: List[int]) -> int:
5-
zero = -1
6-
ones = 0
7-
highest = 0
5+
max_ones = 0
86

9-
for index, num in enumerate(nums):
7+
if not nums:
8+
return max_ones
9+
10+
ones_count = 0
11+
12+
for num in nums:
1013
if num == 1:
11-
ones += 1
12-
elif zero != -1:
13-
total = ones + 1
14-
if total > highest:
15-
highest = total
16-
ones = index - zero - 1
17-
zero = index
14+
ones_count += 1
1815
else:
19-
zero = index
16+
ones_count = 0
2017

21-
last_total = ones
18+
max_ones = max(max_ones, ones_count)
2219

23-
if zero != -1:
24-
last_total += 1
25-
if last_total > highest:
26-
return last_total
27-
return highest
20+
return max_ones
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
4+
def find_max_consecutive_ones_with_flip(nums: List[int]) -> int:
5+
zero = -1
6+
ones = 0
7+
highest = 0
8+
9+
for index, num in enumerate(nums):
10+
if num == 1:
11+
ones += 1
12+
elif zero != -1:
13+
total = ones + 1
14+
if total > highest:
15+
highest = total
16+
ones = index - zero - 1
17+
zero = index
18+
else:
19+
zero = index
20+
21+
last_total = ones
22+
23+
if zero != -1:
24+
last_total += 1
25+
if last_total > highest:
26+
return last_total
27+
return highest

‎arrays/tests/test_find_max_consecutive_ones.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33

44
def test_find_max_consecutive_ones():
5-
assert find_max_consecutive_ones([1, 0]) == 2
6-
assert find_max_consecutive_ones([0]) == 1
5+
assert find_max_consecutive_ones(None) == 0
6+
assert find_max_consecutive_ones([1, 0]) == 1
7+
assert find_max_consecutive_ones([0]) == 0
78
assert find_max_consecutive_ones([1]) == 1
8-
assert find_max_consecutive_ones([1, 0, 1, 1, 0]) == 4
9-
assert find_max_consecutive_ones([1, 0, 1, 1, 1]) == 5
10-
assert find_max_consecutive_ones([0, 1, 1, 0, 1, 0]) == 4
9+
assert find_max_consecutive_ones([1, 0, 1, 1, 0]) == 2
10+
assert find_max_consecutive_ones([1, 0, 1, 1, 1]) == 3
11+
assert find_max_consecutive_ones([0, 1, 1, 0, 1, 0]) == 2
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from arrays.find_max_consecutive_ones_with_flip import find_max_consecutive_ones_with_flip
2+
3+
4+
def test_find_max_consecutive_ones_with_flip():
5+
assert find_max_consecutive_ones_with_flip([1, 0]) == 2
6+
assert find_max_consecutive_ones_with_flip([0]) == 1
7+
assert find_max_consecutive_ones_with_flip([1]) == 1
8+
assert find_max_consecutive_ones_with_flip([1, 0, 1, 1, 0]) == 4
9+
assert find_max_consecutive_ones_with_flip([1, 0, 1, 1, 1]) == 5
10+
assert find_max_consecutive_ones_with_flip([0, 1, 1, 0, 1, 0]) == 4

0 commit comments

Comments
 (0)