Solve in Python:
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

Example 1:


Input: [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.


Example 2:


Input: [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
class Solution:
     def maxProduct(self, nums):
         """
         :type nums: List[int]
         :rtype: int
         """
         n = len(nums)
         dp = [[nums[0], nums[0]] for _ in range(n)]
         ans = nums[0]
         for i in range(1, n):
             temp = [dp[i-1][0] * nums[i], dp[i-1][1] * nums[i], nums[i]]
             dp[i][0], dp[i][1] = max(temp), min(temp)
             if dp[i][0] > ans:
                 ans = dp[i][0]
         return ans
     
 #        n = len(nums)
 #        dp = [[nums[0], nums[0]]] * n   尽量不要使用乘法
 #        ans = nums[0]
 #        for i in range(1, n):
 #            temp = [dp[i-1][0] * nums[i], dp[i-1][1] * nums[i], nums[i]]
 #            dp[i][0], dp[i][1] = max(temp), min(temp)   这样赋值会改变所有值
 #            if dp[i][0] > ans:
 #                ans = dp[i][0]
 #        return dp[-1][0]