1014------算法笔记----------Maximum Product Subarray 最大乘积子数组

1.题目

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.

2.题目分析

  这道题目和之前做过的最大子段和问题很像,因而想到可以用动态规划方法求解,不一样的是,这里求得是乘积,因此要考虑负数和0的情况。

3.解法一

  动态规划的方法一直用的不熟,所以在查资料的过程中发现了一种比较简单的方法,用两个变量maxCurrent和minCurrent表示当前一段内的最大连乘积和最小连乘积,这里之所有要保存最小的因为如果下一个值为负数,那么此时最小的显然就是最大的了。每次都和 maxProduct 和 minProduct比较,并更新他们。

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <limits.h>
using namespace std;

class Solution{
    public:
        int maxProduct(int a[], int n){
            int maxCurrent, minCurrent, maxProduct, minProduct, i;
            //maxCurrent 存储当前最大乘积的候选序列
            //minCurrent 存储当前最小乘积的候选序列

            maxCurrent = minCurrent = 1;
            maxProduct = a[0];      //数组可能为{0};
            minProduct = a[0];

            for(i = 0; i < n; i++){
                maxCurrent *= a[i];
                minCurrent *= a[i];

                if(maxCurrent > maxProduct)
                    maxProduct = maxCurrent;
                if(minCurrent > maxProduct)     //负数 * 负数 的情况
                    maxProduct = minCurrent;

                if(maxCurrent < minProduct)
                    minProduct = maxCurrent;
                if(minCurrent < minProduct)
                    minProduct = minCurrent;

                if(maxCurrent < minCurrent)
                    swap(maxCurrent, minCurrent);

                if(maxCurrent <= 0)
                    maxCurrent = 1;
            }
            return maxProduct;
        }

};

int main(int argc, const char *argv[])
{
    int b[] = {0, 2, 3, -4, -2};
    Solution so;
    cout << so.maxProduct(b, sizeof b/sizeof (int)) << endl;
    return 0;
}

4.解法二

  动态规划方法。

#include <iostream>
#include <string>
#include <vector>
#include <utility>

using namespace std;

class Solution{
    public:
        int maxProduct(int a[], int n){
            int *maxCurrent, *minCurrent, i, maxProduct;
            maxCurrent = new int[n];       //maxCurrent[i] a[0]~a[i]的最大连乘积子数组的值
            minCurrent = new int[n];

            maxCurrent[0] = minCurrent[0] = maxProduct = a[0];

            for(i = 1; i < n; i++){
                maxCurrent[i] = max(max(a[i], maxCurrent[i-1] * a[i]), minCurrent[i-1] * a[i]);
                minCurrent[i] = min(min(a[i], maxCurrent[i-1] * a[i]), minCurrent[i-1] * a[i]);

                maxProduct = max(maxProduct, maxCurrent[i]);
            }
            cout << maxProduct << endl;
        }

};

int main(int argc, const char *argv[])
{
    int a[] = {0};
    Solution so;
    so.maxProduct(a, sizeof a / sizeof(int));

    return 0;
}

5.参考资料

https://oj.leetcode.com/problems/maximum-product-subarray/

http://blog.csdn.net/v_july_v/article/details/8701148

时间: 2024-08-02 18:30:36

1014------算法笔记----------Maximum Product Subarray 最大乘积子数组的相关文章

Maximum Product Subarray 求最大子数组乘积

这个求最大子数组乘积问题是由最大子数组之和问题演变而来,但是却比求最大子数组之和要复杂,因为在求和的时候,遇到0,不会改变最大值,遇到负数,也只是会减小最大值而已.而在求最大子数组乘积的问题中,遇到0会使整个乘积为0,而遇到负数,则会使最大乘积变成最小乘积,正因为有负数和0的存在,使问题变得复杂了不少.. 比如,我们现在有一个数组[2, 3, -2, 4],我们可以很容易的找出所有的连续子数组,[2], [3], [-2], [4], [2, 3], [3, -2], [-2, 4], [2,

[LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6. Example 2: Input: [-2,0,-1]

LintCode刷题笔记-- 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. 解题思路: 前面几道题有点儿过分依赖答案了,后面还是需要自己主动

算法(8)Maximum Product Subarray

题目:在一个数组中找到一个子数组,让子数组的乘积最大,比如[2,3,-2,4]返回6 思路:之前自己想到的思路是对于一个int类型的数组,只要负数的个数是偶数,那么乘积肯定是全局乘就可以了,然后对于负数个数是奇数的情况,那么我们就找到最前和最后的一个负数,然后分别计算其前后子数组的乘积就可以了!因为其前后子数组的个数必然是偶数!想到这里还心里窃喜,感觉自己发现了一个莫大的规律,但是分分钟被教做人呢!因为上面红线标注的部分实现起来格外麻烦,但是自己的思维里面还是少了一步!那就是既然你已经想到了要么

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

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

152. Maximum Product Subarray(js)

152. Maximum Product Subarray Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: Input: [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product

刷题152. Maximum Product Subarray

一.题目说明 题目152. Maximum Product Subarray,给一列整数,求最大连续子序列,其乘积最大.难度是Medium! 二.我的解答 这个题目,用双重循环就可以了. class Solution{ public: int maxProduct(vector<int>& nums){ if(nums.size()<=1) return nums[0]; product = INT_MIN; int len = nums.size(); for(int i=0;

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution 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