My python code using two list


  • 2
    Q
    def maxProduct(self, nums):
        dp = [nums[0]] * len(nums)
        dp_min = [nums[0]] * len(nums)
        for i in range(1, len(nums)):
            if dp[i-1] * nums[i] <= 0:
                dp[i] = max(nums[i], dp_min[i-1] * nums[i])
            else:
                dp[i] = max(dp[i-1] * nums[i], dp_min[i-1] * nums[i])
                
            dp_min[i] = min(dp_min[i-1] * nums[i], dp[i-1] * nums[i], nums[i])
        return max(dp)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.