码迷,mamicode.com
首页 > 编程语言 > 详细

[leetcode] Maximum Product Subarray @ python

时间:2014-09-28 01:51:00      阅读:499      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   strong   for   sp   div   

[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

标签:style   blog   color   io   ar   strong   for   sp   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!