[C + +] leetcode:96 Maximum Product subarray (Dynamic planning)

Source: Internet
Author: User

Title:

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 .

Ideas:

This problem is similar to maximum subarray solution, maintaining two variables for dynamic programming, a local optimal, and a global optimal. The local optimality of step I is expressed as the local optimal solution which must contain a[i-1] (the current element), and the global optimum is the optimal value up to the current element. To discuss the recurrence of dynamic programming, assuming that we know global[i] and local[i], step i+1: local[i+1] = max (A[i], local[i]+a[i]), local optimality must contain the current element, So is the local optimization of the previous step (including the previous array element) local[i]+ current element A[i] (Local[i] contains the first element, A[i] is the i+1 element, so that the calculation is in accordance with the set), if Local[i] is a negative value, plus it cannot guarantee the current local optimal, So take a[i]. GLOBAL[I+1] = max (Global[i], local[i+1]). With the local optimization of the current step, the global optimal is the current local optimal or the original global optimal (the global optimal always maintains the optimal solution of all solutions, including all cases, if the optimal solution does not contain the current element, then the front will be maintained in the global optimal solution, if the current element is included, The solution at this time is local optimization)

Based on the above analysis, we can get the AC Code of Maximum subarray :

Class Solution {public:    int maxsubarray (int a[], int n) {        if (n = = 0) return 0;        int local = a[0];        int global = a[0];                for (int i = 1; i < n; i++)        {            local = max (local + a[i], a[i]);            Global = MAX (global, local);        }                return global;    }};

Now we analyze the problem, this problem model and ideas are very similar to the above question, or a one-dimensional dynamic programming in the "local optimal and global optimal method." The difference is that we need to consider the characteristics of multiplication, just to maintain a local optimal insufficiency to get the global optimal behind. Because multiplication and addition are different, as long as the cumulative result is a positive increment, the multiplication may be negative on the previous result, and then appear (do not need adjacent) another negative number multiplied to get a greater product (negative negative is positive). So we need to maintain three variables, local maximum, local minimum, global maximum. The local largest definition, maintained from 0~k, contains the local maximum of the current element (must contain k).

k.

Next, let's take a look at the recursive formula: Suppose we get the current local maximum maxlocal[i] of step I, local minimum minlocal[i], global maximum global[i]. The local maximum maxlocal[i+1 of step i+1 ] = max (A[i], Maxlocal[i] * a[i], minlocal[i] * a[i]), minlocal[i+1] = min (A[i], maxlocal[i] *a[i], minlocal[i]*a[i]), global[i+1] = max (maxlocal, Global); given the recursive formula, we can then hit the code. This problem is a good interview topic, the above topic is more common, the problem model is similar, and there are new test centers. can also examine the dynamic planning, we need to face the specific problems, careful analysis and thinking, thoroughly understand the method, can do to raise one and anti-three.

Attention:

1. Note that the multiplication is considered in this problem, and it is necessary to consider the characteristics of multiplication and to maintain a variable minlocal.

2. Note that when we are dynamically planning, the last maxlocal[i] is used when solving minlocal[i+1], so we need to save the maxlocal[i before the local optimal update]

int maxcopy = maxlocal;            maxlocal = Max (max (Maxlocal*a[i], a[i]), minlocal*a[i]);            minlocal = min (min (maxcopy*a[i], a[i]), minlocal*a[i]);            Global = MAX (maxlocal, Global);

3. Special circumstances, cannot forget, if the array has no elements, return 0.

4. The parameters of the max and Min functions are two values and can only be compared with two values, so the function is called two times.

maxlocal = Max (max (Maxlocal*a[i], a[i]), minlocal*a[i]); minlocal = min (min (maxcopy*a[i], a[i]), minlocal*a[i]);

Complexity: O (N)

AC Code:

Class Solution {public:    int maxproduct (int a[], int n) {        if (n = = 0) return 0;        if (n = = 1) return a[0];                int maxlocal = a[0];        int minlocal = a[0];        int global = a[0];                for (int i = 1; i < n; i++)        {            int maxcopy = maxlocal;            maxlocal = Max (max (Maxlocal*a[i], a[i]), minlocal*a[i]);            minlocal = min (min (maxcopy*a[i], a[i]), minlocal*a[i]);            Global = MAX (maxlocal, Global);        }                return global;    }};



[c++]leetcode:96 Maximum Product subarray (Dynamic planning)

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.