标签:
题目描述:
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4]
,
the contiguous subarray [2,3]
has the largest product = 6
.
Subscribe to see which companies asked this question
1 class Solution(object): 2 def maxProduct(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 ln = len(nums) 8 maxPro = [0]*(ln+1) 9 minPro = [0]*(ln+1) 10 maxPro[0] = 1 11 minPro[0] = 1 12 for i in range(0, ln): 13 if nums[i] >= 0: 14 maxPro[i+1] = max(maxPro[i]*nums[i], nums[i]) 15 minPro[i+1] = min(minPro[i]*nums[i], nums[i]) 16 else: 17 minPro[i+1] = min(maxPro[i]*nums[i], nums[i]) 18 maxPro[i+1] = max(minPro[i]*nums[i], nums[i]) 19 return max(maxPro[1:])
leetcode 之Maximum Product Subarray
标签:
原文地址:http://www.cnblogs.com/missmzt/p/5584649.html