1014------Algorithm Note----------Maximum product Subarray Maximum product sub-array

Source: Internet
Author: User

1. Topics

Find the contiguous subarray within an array (containing at least one number) which have the largest product.

For example, given the array [2,3,-2,4] ,
The contiguous Subarray has the [2,3] largest product = 6 .

2. Topic analysis

This problem is similar to the largest sub-segment and question that has been done before, so think of the dynamic programming method to solve, not the same, here is the product, so consider negative numbers and 0 of the case.

3. Solution One

Dynamic programming method has been used not familiar, so in the process of checking the data found a relatively simple method, with two variables maxcurrent and mincurrent to represent the current period of the maximum number of continuous product and the minimum product, here all to save the least because if the next value is negative, At this point the smallest is obviously the biggest. Compare with Maxproduct and minproduct each time, and update them.

#include <iostream> #include <string> #include <vector> #include <utility> #include < Limits.h>using namespace Std;class solution{public:int maxproduct (int a[], int n) {int Maxcurren            T, Mincurrent, maxproduct, minproduct, I;            Maxcurrent a candidate sequence that stores the current maximum product//mincurrent a candidate sequence that stores the current minimum product maxcurrent = Mincurrent = 1;      Maxproduct = a[0];            The array may be {0}; minproduct = a[0];                for (i = 0; i < n; i++) {maxcurrent *= a[i];                Mincurrent *= A[i];                if (Maxcurrent > maxproduct) maxproduct = maxcurrent;                if (Mincurrent > Maxproduct)//Negative number * Negative case 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. Solution Two

Dynamic planning methods.

 #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] The value of the largest subarray of 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. References

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

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

1014------Algorithm Note----------Maximum product Subarray Maximum product sub-array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.