[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
时间: 2024-10-27 00:05:48

[leetcode] Maximum Product Subarray @ python的相关文章

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

LeetCode: Maximum Product Subarray && Maximum Subarray

Maximum Product Subarray Title: 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. 对于Product

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/max

[LeetCode] 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. 这题和之前的一题Maximum Subarray非常类似,一个是求最大和,而这个是

[LeetCode]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. 这道题是找出数组中的一个子序列,要求这个子序列中数的乘积是所有子序列中最大的. 如

[LeetCode] 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. Hide Tags Array Dynamic Programming 这个问题是给

LeetCode Maximum Product Subarray 解题报告

LeetCode 新题又更新了,最大子数组乘积 https://oj.leetcode.com/problems/maximum-product-subarray/ 题目分析:求一个数组,连续子数组的最大乘积. 解题思路:最简单的思路就是3重循环,求解productArray[i][j]的值(productArray[i][j]为A[i]到A[j]的乘积),然后记录其中最大值,算法时间复杂度O(n3),必然TLE. 第一次优化,动态规划,求解:productArray[i][j]的时候不用再次循

DP Leetcode - Maximum Product Subarray

最近一直忙着写paper,很久没做题,一下子把题目搞复杂了..思路理清楚了非常简单,每次只需更新2个值:当前子序列最大乘积和当前子序列的最小乘积.最大乘积被更新有三种可能:当前A[i]>0,乘以前面最大的数(>0),得到新的最大乘积:当前A[i]<0,乘以前面最小的数(<0),得到新的最大乘积:A[i]它自己>0,(A[i-1]==0.最小乘积同理.. class Solution { public: int Max(int a, int b, int c) { int ma

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 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 =