Medium
, Dynamic Programming
Question
找到积最大的子序列
For example
序列[2,3,-2,4]的最大积序列为[2,3]积为6
Solution
与子序列最大和类似,不过需要考虑局部最大积和局部最小积,因为局部最小积为负数时与一个负数相乘可能成为最大积
class Solution(object):
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maximum, minimum, maxAns = nums[0], nums[0], nums[0]
for i in range(1,len(nums)):
mx,mn = maximum,minimum
maximum = max(max(nums[i],mx*nums[i]),mn*nums[i])
minimum = min(min(nums[i],mx*nums[i]),mn*nums[i])
maxAns = max(maximum,maxAns)
return maxAns