首页 > 代码库 > [leetcode]Maximum Product Subarray @ Python

[leetcode]Maximum Product Subarray @ Python

原题地址:https://oj.leetcode.com/problems/maximum-product-subarray/

解题思路:主要需要考虑负负得正这种情况,比如之前的最小值是一个负数,再乘以一个负数就有可能成为一个很大的正数。

代码:

class Solution:    # @param A, a list of integers    # @return an integer    def maxProduct(self, A):        if len(A) == 0:            return 0        min_tmp = A[0]        max_tmp = A[0]        result = A[0]        for i in range(1, len(A)):            a = A[i] * min_tmp            b = A[i] * max_tmp            c = A[i]            max_tmp = max(max(a,b),c)            min_tmp = min(min(a,b),c)            result = max_tmp if max_tmp > result else result        return result        

 

[leetcode]Maximum Product Subarray @ Python