Skip to content

Commit a004929

Browse files
rohan472000cclausspre-commit-ci[bot]
authored
added a problem on kadane's algo and its solution. (TheAlgorithms#8569)
* added kadane's algorithm directory with one problem's solution. * added type hints * Rename kaadne_algorithm/max_product_subarray.py to dynamic_programming/max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py * Update max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * Update max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py --------- Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a71f22d commit a004929

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
def max_product_subarray(numbers: list[int]) -> int:
2+
"""
3+
Returns the maximum product that can be obtained by multiplying a
4+
contiguous subarray of the given integer list `nums`.
5+
6+
Example:
7+
>>> max_product_subarray([2, 3, -2, 4])
8+
6
9+
>>> max_product_subarray((-2, 0, -1))
10+
0
11+
>>> max_product_subarray([2, 3, -2, 4, -1])
12+
48
13+
>>> max_product_subarray([-1])
14+
-1
15+
>>> max_product_subarray([0])
16+
0
17+
>>> max_product_subarray([])
18+
0
19+
>>> max_product_subarray("")
20+
0
21+
>>> max_product_subarray(None)
22+
0
23+
>>> max_product_subarray([2, 3, -2, 4.5, -1])
24+
Traceback (most recent call last):
25+
...
26+
ValueError: numbers must be an iterable of integers
27+
>>> max_product_subarray("ABC")
28+
Traceback (most recent call last):
29+
...
30+
ValueError: numbers must be an iterable of integers
31+
"""
32+
if not numbers:
33+
return 0
34+
35+
if not isinstance(numbers, (list, tuple)) or not all(
36+
isinstance(number, int) for number in numbers
37+
):
38+
raise ValueError("numbers must be an iterable of integers")
39+
40+
max_till_now = min_till_now = max_prod = numbers[0]
41+
42+
for i in range(1, len(numbers)):
43+
# update the maximum and minimum subarray products
44+
number = numbers[i]
45+
if number < 0:
46+
max_till_now, min_till_now = min_till_now, max_till_now
47+
max_till_now = max(number, max_till_now * number)
48+
min_till_now = min(number, min_till_now * number)
49+
50+
# update the maximum product found till now
51+
max_prod = max(max_prod, max_till_now)
52+
53+
return max_prod

0 commit comments

Comments
 (0)