I have come up with the following solution to find the maximum product for a contiguous sub-array:
def maxProduct(nums):
max_prod = [0]*len(nums)
min_prod = [0]*len(nums)
for i in range(0, len(nums)):
min_prod[i] = min(nums[i], min_prod[i-1]*nums[i], max_prod[i-1]*nums[i])
max_prod[i] = max(nums[i], min_prod[i-1]*nums[i], max_prod[i-1]*nums[i])
return max(max_prod)
Current solution is O(n)
in space, trying to find O(1)
solution for space, but I keep seem to missing it. Ideas?