-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added a problem on kadane's algo and its solution. #8569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6209eb7
added kadane's algorithm directory with one problem's solution.
rohan472000 767b1d0
added type hints
rohan472000 a5ea4b2
Rename kaadne_algorithm/max_product_subarray.py to dynamic_programmin…
rohan472000 28b4406
Update dynamic_programming/max_product_subarray.py
rohan472000 71ceff4
Update max_product_subarray.py
rohan472000 0a6d492
Update max_product_subarray.py
rohan472000 0d4a9aa
Update dynamic_programming/max_product_subarray.py
rohan472000 b841831
Update max_product_subarray.py
rohan472000 88aeeba
Update max_product_subarray.py
rohan472000 caea207
Update max_product_subarray.py
rohan472000 4f63025
Update max_product_subarray.py
rohan472000 39ac3cb
Update max_product_subarray.py
rohan472000 ea6a16c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 45e5d97
Update max_product_subarray.py
rohan472000 9e3d158
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5fa1095
Update max_product_subarray.py
rohan472000 12eb7e4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dc92eb7
Update max_product_subarray.py
rohan472000 58df091
Update max_product_subarray.py
rohan472000 889163b
Update dynamic_programming/max_product_subarray.py
rohan472000 3fc72c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 87880bb
Update dynamic_programming/max_product_subarray.py
rohan472000 5437ee3
Update max_product_subarray.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
def max_product_subarray(numbers: list[float]) -> float: | ||
""" | ||
Returns the maximum product that can be obtained by multiplying a | ||
contiguous subarray of the given integer list `nums`. | ||
|
||
Example: | ||
>>> max_product_subarray([2, 3, -2, 4]) | ||
6 | ||
>>> max_product_subarray([-2, 0, -1]) | ||
0 | ||
>>> max_product_subarray([2, 3, -2, 4, -1]) | ||
48 | ||
>>> max_product_subarray([2, 3, -2, 4.5, -1]) | ||
ValueError: numbers must be an iterable of integers | ||
rohan472000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> max_product_subarray([-1]) | ||
-1 | ||
>>> max_product_subarray([]) | ||
0 | ||
>>> max_product_subarray("ABC") | ||
0 | ||
>>> max_product_subarray("") | ||
0 | ||
>>> max_product_subarray(None) | ||
0 | ||
""" | ||
if numbers is None or not isinstance(numbers, list): | ||
rohan472000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return 0 | ||
|
||
if len(numbers) == 0: | ||
return 0 | ||
|
||
if not isinstance(numbers, (list, set, tuple)) or not all( | ||
isinstance(number, int) for number in numbers | ||
): | ||
raise ValueError("numbers must be an iterable of integers") | ||
|
||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
max_till_now = min_till_now = max_prod = numbers[0] | ||
|
||
for i in range(1, len(numbers)): | ||
# update the maximum and minimum subarray products | ||
number = numbers[i] | ||
if number < 0: | ||
max_till_now, min_till_now = min_till_now, max_till_now | ||
max_till_now = max(number, max_till_now * number) | ||
min_till_now = min(number, min_till_now * number) | ||
|
||
# update the maximum product found till now | ||
max_prod = max(max_prod, max_till_now) | ||
|
||
return max_prod |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.