Skip to content

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 23 commits into from
Mar 31, 2023
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 Mar 29, 2023
767b1d0
added type hints
rohan472000 Mar 29, 2023
a5ea4b2
Rename kaadne_algorithm/max_product_subarray.py to dynamic_programmin…
rohan472000 Mar 30, 2023
28b4406
Update dynamic_programming/max_product_subarray.py
rohan472000 Mar 30, 2023
71ceff4
Update max_product_subarray.py
rohan472000 Mar 30, 2023
0a6d492
Update max_product_subarray.py
rohan472000 Mar 30, 2023
0d4a9aa
Update dynamic_programming/max_product_subarray.py
rohan472000 Mar 30, 2023
b841831
Update max_product_subarray.py
rohan472000 Mar 30, 2023
88aeeba
Update max_product_subarray.py
rohan472000 Mar 30, 2023
caea207
Update max_product_subarray.py
rohan472000 Mar 30, 2023
4f63025
Update max_product_subarray.py
rohan472000 Mar 30, 2023
39ac3cb
Update max_product_subarray.py
rohan472000 Mar 30, 2023
ea6a16c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 30, 2023
45e5d97
Update max_product_subarray.py
rohan472000 Mar 30, 2023
9e3d158
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 30, 2023
5fa1095
Update max_product_subarray.py
rohan472000 Mar 30, 2023
12eb7e4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 30, 2023
dc92eb7
Update max_product_subarray.py
rohan472000 Mar 30, 2023
58df091
Update max_product_subarray.py
rohan472000 Mar 31, 2023
889163b
Update dynamic_programming/max_product_subarray.py
rohan472000 Mar 31, 2023
3fc72c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 31, 2023
87880bb
Update dynamic_programming/max_product_subarray.py
rohan472000 Mar 31, 2023
5437ee3
Update max_product_subarray.py
cclauss Mar 31, 2023
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
50 changes: 50 additions & 0 deletions dynamic_programming/max_product_subarray.py
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
>>> 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):
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")

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