[leetcode] Maximum Product Subarray @ python

[leetcode] Latest added: 2014-09-23

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
原文地址:https://www.cnblogs.com/asrman/p/3997388.html