diff --git a/README.md b/README.md index 38d97a0..2a6d3c4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/arrays/README.md b/arrays/README.md index d9c400b..a602722 100644 --- a/arrays/README.md +++ b/arrays/README.md @@ -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) @@ -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]` @@ -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: @@ -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 diff --git a/arrays/find_max_consecutive_ones.py b/arrays/find_max_consecutive_ones.py index 936598d..bc63c93 100644 --- a/arrays/find_max_consecutive_ones.py +++ b/arrays/find_max_consecutive_ones.py @@ -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 diff --git a/arrays/find_max_consecutive_ones_with_flip.py b/arrays/find_max_consecutive_ones_with_flip.py new file mode 100644 index 0000000..41e79d5 --- /dev/null +++ b/arrays/find_max_consecutive_ones_with_flip.py @@ -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 diff --git a/arrays/tests/test_find_max_consecutive_ones.py b/arrays/tests/test_find_max_consecutive_ones.py index 65955ef..8eeb4f6 100644 --- a/arrays/tests/test_find_max_consecutive_ones.py +++ b/arrays/tests/test_find_max_consecutive_ones.py @@ -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 diff --git a/arrays/tests/test_find_max_consecutive_ones_with_flip.py b/arrays/tests/test_find_max_consecutive_ones_with_flip.py new file mode 100644 index 0000000..bf9934a --- /dev/null +++ b/arrays/tests/test_find_max_consecutive_ones_with_flip.py @@ -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