<jupyter_start><jupyter_text>给定一个整数数组nums，在一个数组（至少包含一个数字）中找到具有最大乘积的连续子数组。    例1：        Input: [2,3,-2,4]        Output: 6        Explanation: [2,3] has the largest product 6.    例2：        Input: [-2,0,-1]        Output: 0        Explanation: The result cannot be 2, because [-2,-1] is not a subarray.     1、同时记录下，当前数组中的最大值和最小值    2、如果当前值是负数，就用负数乘于DP中的 前一个保存的最小值<jupyter_code>class Solution:
    def maxProduct(self, nums) -> int:
        if len(nums) == 0: return 0
        if len(nums) == 1: return nums[0]
        # 全是正数，就是整个数组的乘积和
        if min(nums) > 0: 
            result = 1
            for num in nums:
                result *= num
            return result
        dp = [[1, 1]] * len(nums)
        dp[0] = [nums[0], nums[0]] #dp[0][0] indicate max and dp[0][1] indicate min
        for i in range(1, len(nums)):
            if nums[i] == 0:
                dp[i] = [0, 0]
            elif nums[i] > 0:
                cur_max = max(max(dp[i-1]) * nums[i], nums[i])
                cur_min = min(min(dp[i-1]) * nums[i], nums[i])
                dp[i] = [cur_max, cur_min]
            else:
                cur_max = max(min(dp[i-1]) * nums[i], nums[i])
                cur_min = min(max(dp[i-1]) * nums[i], nums[i])
                dp[i] = [cur_max, cur_min]
        return max([x[0] for x in dp])
nums_ = [2,5,2,-1,3]
solution = Solution()
solution.maxProduct(nums_)<jupyter_output>[[2, 2], [10, 5], [20, 2], [-1, -20], [3, -60]]
{'fraction_non_alphanumeric': 0.16481994459833796, 'fraction_numerical': 0.04362880886426593, 'mean_word_length': 1.8957915831663326, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '312043', 'license': 'Apache-2.0', 'n_tokens_mistral': 644, 'n_tokens_neox': 628, 'n_words': 134, 'path': 'Dynamic Programming/past/152. Maximum Product Subarray.ipynb', 'repo_name': 'YuHe0108/Leetcode', 'size': '1444'}