Maximum Product Subarray
Description:
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example
For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6.
Code:
class Solution:
"""
@param nums: An array of integers
@return: An integer
"""
def maxProduct(self, nums):
# write your code here
if not nums:
return 0
if len(nums)==1:
return nums[0]
res = 1
minV = 1
maxV = 1
for i in range(len(nums)):
minV, maxV = min(nums[i], nums[i]*minV, nums[i]*maxV), max(nums[i], nums[i]*minV, nums[i]*maxV)
res = max(res, maxV)
return res