标签:style blog color io ar strong for sp div
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
.
Trick: Save Min value and Max value since the max value are only related with extrem value and current value
class Solution: # @param A, a list of integers # @return an integer def maxProduct(self, A): minTemp = maxTemp = MAX = A[0] for i in range(1, len(A)): Temp = [A[i], A[i] * minTemp, A[i] * maxTemp] minTemp, maxTemp = min(Temp), max(Temp) MAX = max(maxTemp,MAX) return MAX
[leetcode] Maximum Product Subarray @ python
标签:style blog color io ar strong for sp div
原文地址:http://www.cnblogs.com/asrman/p/3997388.html