[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.

这道题是找出数组中的一个子序列,要求这个子序列中数的乘积是所有子序列中最大的。

如果不考虑效率与优化问题,可以直接将所有子序列的组合列举出来,分别求它们的积,取最大值。如下:

int maxProduct(int A[], int n) {
        int i = 0, j = 0,k=0;
        int ans = 0;
        int product = 0;
        for (i = n; i > 0; i--){
            for (j = 0; j <= n-i; j++){
                for (k = j; k < i; k++){
                    product += A[k];
                }
                if (product>ans){
                    ans = product;
                }
                product = 0;
            }
        }
        return ans;
    }

但是这样的效率是很低的。换个思路可以想到,这道题就是一维动态规划中的“局部最优与全局最优”。所以需要维护两个变量,当前位置连续乘积的最大值curMax和最小值curMin。

最大值与最小值由以下三种情况可以得到:上一次的curMax*A[i],上一次的curMin*A[i],A[i]本身。如下:

int maxProduct(int A[], int n) {
        int ans = A[0];
        int curMin = A[0];
        int curMax = A[0];
        int cur = 1;
        for (int i = 1; i < n; i++){
            cur = curMax;
            curMax = max(max(A[i],A[i]*curMax),A[i]*curMin);
            curMin = min(min(A[i], A[i] * cur), A[i] * curMin);
            ans = max(ans, curMax);
        }
        return ans;
    }
时间: 2024-10-27 00:05:43

[LeetCode]Maximum Product Subarray的相关文章

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 &amp;&amp; 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 @ 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:

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. 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 =