Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ to prevent obscuring the function and logic of the algorithms / data-structures.

### Local development

- Install `pyenv`
- Install current Python version: `pyenv install 3.12`
- Set global Python version: `pyenv global 3.12`
- Initialize virtual environment: `python -m venv .venv`
- Activate: `source .venv/bin/activate`
- Install packages: `pip3 install -r requirements.txt`
* Install `pyenv`
* Install current Python version: `pyenv install 3.12`
* Set global Python version: `pyenv global 3.12`
* Initialize virtual environment: `python -m venv .venv`
* Activate: `source .venv/bin/activate`
* Install packages: `pip install -r requirements.txt`

### VS Code Dev Container

- Download VSCode and Docker
- Launch project inside of Docker container in VSCode
- VSCode does the rest!
* Download VSCode and Docker
* Launch project inside of Docker container in VSCode
* VSCode does the rest!

## Project Commands

Expand Down
41 changes: 39 additions & 2 deletions arrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [Find Disappeared Numbers](#find-disappeared-numbers)
* [Find Index of Largest Number](#find-index-of-largest-number)
* [Find Max Consecutive Ones](#find-max-consecutive-ones)
* [Find Max Consecutive Ones With One Flip](#find-max-consecutive-ones-with-1-flip)
* [Find Smallest Positive Integer](#find-smallest-positive-integer)
* [First Duplicate](#first-duplicate)
* [First Not Repeating Character](#first-not-repeating-character)
Expand Down Expand Up @@ -44,7 +45,7 @@ More formally check if there exists two indices i and j such that :

`i != j`
`arr[i] == 2 * arr[j]`

Example 1:

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

## Find Max Consecutive Ones

You are given a binary array nums, return the maximum number of consecutive 1's in the array.

Example 1:

Input: nums = [1,1,0,1,1,1]

Output: 3

Example 2:

Input: nums = [1,0,1,1,0,1]

Output: 2

```python
def find_max_consecutive_ones(nums: List[int]) -> int:
max_ones = 0

if not nums:
return max_ones

ones_count = 0

for num in nums:
if num == 1:
ones_count += 1
else:
ones_count = 0

max_ones = max(max_ones, ones_count)

return max_ones
```

## Find Max Consecutive Ones With 1 Flip

Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.

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

```python
def find_max_consecutive_ones(nums: List[int]) -> int:
def find_max_consecutive_ones_with_flip(nums: List[int]) -> int:
zero = -1
ones = 0
highest = 0
Expand Down
29 changes: 11 additions & 18 deletions arrays/find_max_consecutive_ones.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@


def find_max_consecutive_ones(nums: List[int]) -> int:
zero = -1
ones = 0
highest = 0
max_ones = 0

for index, num in enumerate(nums):
if not nums:
return max_ones

ones_count = 0

for num in nums:
if num == 1:
ones += 1
elif zero != -1:
total = ones + 1
if total > highest:
highest = total
ones = index - zero - 1
zero = index
ones_count += 1
else:
zero = index
ones_count = 0

last_total = ones
max_ones = max(max_ones, ones_count)

if zero != -1:
last_total += 1
if last_total > highest:
return last_total
return highest
return max_ones
27 changes: 27 additions & 0 deletions arrays/find_max_consecutive_ones_with_flip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import List


def find_max_consecutive_ones_with_flip(nums: List[int]) -> int:
zero = -1
ones = 0
highest = 0

for index, num in enumerate(nums):
if num == 1:
ones += 1
elif zero != -1:
total = ones + 1
if total > highest:
highest = total
ones = index - zero - 1
zero = index
else:
zero = index

last_total = ones

if zero != -1:
last_total += 1
if last_total > highest:
return last_total
return highest
11 changes: 6 additions & 5 deletions arrays/tests/test_find_max_consecutive_ones.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@


def test_find_max_consecutive_ones():
assert find_max_consecutive_ones([1, 0]) == 2
assert find_max_consecutive_ones([0]) == 1
assert find_max_consecutive_ones(None) == 0
assert find_max_consecutive_ones([1, 0]) == 1
assert find_max_consecutive_ones([0]) == 0
assert find_max_consecutive_ones([1]) == 1
assert find_max_consecutive_ones([1, 0, 1, 1, 0]) == 4
assert find_max_consecutive_ones([1, 0, 1, 1, 1]) == 5
assert find_max_consecutive_ones([0, 1, 1, 0, 1, 0]) == 4
assert find_max_consecutive_ones([1, 0, 1, 1, 0]) == 2
assert find_max_consecutive_ones([1, 0, 1, 1, 1]) == 3
assert find_max_consecutive_ones([0, 1, 1, 0, 1, 0]) == 2
10 changes: 10 additions & 0 deletions arrays/tests/test_find_max_consecutive_ones_with_flip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from arrays.find_max_consecutive_ones_with_flip import find_max_consecutive_ones_with_flip


def test_find_max_consecutive_ones_with_flip():
assert find_max_consecutive_ones_with_flip([1, 0]) == 2
assert find_max_consecutive_ones_with_flip([0]) == 1
assert find_max_consecutive_ones_with_flip([1]) == 1
assert find_max_consecutive_ones_with_flip([1, 0, 1, 1, 0]) == 4
assert find_max_consecutive_ones_with_flip([1, 0, 1, 1, 1]) == 5
assert find_max_consecutive_ones_with_flip([0, 1, 1, 0, 1, 0]) == 4
Loading