首页 > ACM题库 > LeetCode > LeetCode-Maximum Product Subarray题解
2014
11-18

LeetCode-Maximum Product Subarray题解

Maximum Product Subarray

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.

标签: Array Dynamic Programming
分析

Java代码:

class Solution:
    def maxProduct(self, A):
        maxHere = 1
        minHere = 1
        ans = -0x7fffffff
        for x in A:
            tmp = minHere
            minHere = min(x, x*minHere, x*maxHere)
            maxHere = max(x, x*tmp, x*maxHere)
            ans = max(ans,maxHere)
        return ans

  1. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?