152. Maximum Product Subarray

动态规划的做法,转移方程真的很难写
我至今还是糊糊涂涂的

class Solution(object):
    def maxProduct(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if len(nums) == 0:
            return 0
        if len(nums) == 1:
            return nums[0]
        #动态规划
        maxlast = nums[0]
        minlast = nums[0]
        maxproduct = nums[0]
        for i in range(1,len(nums)):
            maxcurr = max(nums[i], max(maxlast*nums[i], minlast*nums[i]))
            mincurr = min(nums[i], min(maxlast*nums[i], minlast*nums[i]))
            minlast = mincurr
            maxlast = maxcurr
            maxproduct = max(maxcurr, maxproduct)
        return maxproduct

你可能感兴趣的:(152. Maximum Product Subarray)