mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
f97d07ebd3
commit
9d6b99bdae
@ -1,23 +1,24 @@
|
|||||||
'''
|
"""
|
||||||
Maximum Product Subarray
|
Maximum Product Subarray
|
||||||
This algorithm finds the contiguous subarray within an array that has the largest product.
|
This algorithm finds the contiguous subarray within an array that has the largest product.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
|
|
||||||
def maxProoductSubarray(nums, n):
|
def maxProoductSubarray(nums, n):
|
||||||
ans = nums[0]
|
ans = nums[0]
|
||||||
|
|
||||||
for i in range(n):
|
for i in range(n):
|
||||||
mul = nums[i]
|
mul = nums[i]
|
||||||
for j in range(i + 1, n):
|
for j in range(i + 1, n):
|
||||||
ans = max(ans, mul)
|
ans = max(ans, mul)
|
||||||
mul *= nums[j]
|
mul *= nums[j]
|
||||||
|
|
||||||
# changing the result for index n-1th
|
# changing the result for index n-1th
|
||||||
ans = max(ans, mul)
|
ans = max(ans, mul)
|
||||||
|
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
nums = [1, -2, -3, 0, 7, -8, -2]
|
nums = [1, -2, -3, 0, 7, -8, -2]
|
||||||
n = len(nums)
|
n = len(nums)
|
||||||
print("Maximum Sub array is ", maxProoductSubarray(nums, n))
|
print("Maximum Sub array is ", maxProoductSubarray(nums, n))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user