首页 > 编程语言 > 详细

[leetcode] Maximum Product Subarray @ python

时间:2014-09-27 23:52:11      阅读:406      评论:0      收藏:0      [点我收藏+]

[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

 

[leetcode] Maximum Product Subarray @ python

原文:http://www.cnblogs.com/asrman/p/3997388.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!