"Leetcode" Maximum product Subarray to make successive sub-arrays the largest product

Source: Internet
Author: User

ADD Date 2014-09-23

Maximum Product Subarray

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 .

In simple terms, find a contiguous subarray in an int array, making it the largest product, with positive, negative, and 0 in the array. Do not consider the product too large out of range.

have done before to find continuous sub-array to add and the largest, relatively simple, see "Jian refers to offer" Q31.

At first, I also tried to find ideas from that problem and found it was not very scientific ... Then I found a way to share the idea.

First, any number is multiplied by 0 to get 0, and in the case of 0, because the number is an integer, the absolute value of the product is not smaller; again, the negative is positive.

Based on these three points, first of all based on the idea of recursion with 0 segments, that is, the array A is split into [an array without 0], [0], [the remainder of the array], and set rel1 for the sub-array does not contain 0 the maximum product, Rel2 for the remaining array to get the maximum product, then the array a The greatest product of this is Max{rel1,0,rel2}. Rel2 recursion in this way to get.

Then, the maximum product of an array that does not contain 0 is asked. Because negative negative is positive, it can be thought that if the number of negative numbers is even, then all the numbers multiplied is that maximum product; If the number of negative numbers is odd, then it must be a negative number of the left all the numbers product or the right all the number product is the largest, so you can iterate back the array, and multiply all the elements, with NUMALL1 records this product, while each update NUM1 is the largest product of the pre-to-post multiplication process, and then iterates forward the array from the back, using the NUMALL2 to record the product, num2 the maximum value, so that the maximum product of the entire array is max{num1,num2}.

If you don't understand why a sub-array of the maximum product must be a contiguous subarray from the first number, or a contiguous subarray from the last number, you can think of:

If this product is the largest sub-array is the middle of a paragraph, of course, excluding only one element and a negative number of cases, then this product must be positive (this does not understand the words to think about it, simple), if

1. The left side (right side) of this array is fully positive, so the result of multiplying all the numbers on the left (right) must not be smaller than the current result, so it can be contiguous from the left (right), so there must be a negative number on the left and right side of this array!

2. The left (right) of this array has an even number of negative numbers, the same 1 is not possible, and if there are multiple negative numbers, there must be an even number can be included in the middle of this section of the array to make the product larger; so there must be a negative number on the left and right side!

3. If there is a negative number on the left and right, then there are two negative numbers, so the result of multiplying the entire array must be no less than the middle of the array.

This algorithm iterates through the entire array three times, with the complexity of O (n).

Finally the logic is finished, the individual feel very verbose, just want to explain the white point, do not know everyone has not understand, the following with code, welcome other ideas.

1 classSolution {2  Public:3     intMaxProductNo0 (intA[],intN) {//no 0 array for maximum product4         intNUM1 = a[0];5         intNUMALL1 = a[0];6          for(inti =1; I < n; ++i) {7NumAll1 *=A[i];8NUM1 = numAll1 > NUM1?numall1:num1;9         }Ten          One         intnum2 = a[n-1]; A         intNumAll2 = a[n-1]; -          for(inti = n2; I >=0; --i) { -NumAll2 *=A[i]; thenum2 = numAll2 > num2?numall2:num2; -         } -         returnNum1 > num2?num1:num2; -     } +      -     intMaxproduct (intA[],intN) {//To find the maximum product of an array +         if(A = = NULL | | n <1) A             return 0; at         intindex0 =0; -         intRel1 = a[0]; -         intRel2 = a[0]; -         BOOLHAVE0 =false; -          for(; index0 < n; + +index0) { -             if(a[index0] = =0) { inHAVE0 =true; -                  Break; to             } +         } -         if(Index0 >0) theRel1 = MaxProductNo0 (A, index0);//The maximum product of an array without 0 *         if(n-index0-1>0) $Rel2 = Maxproduct (a+index0+1, n-index0-1);//The maximum product of the remaining arrayPanax NotoginsengRel1 = rel1 > Rel2?Rel1:rel2; -         if(HAVE0) theRel1 = rel1 >0? Rel1:0; +         returnRel1; A     } the};

"Leetcode" Maximum product Subarray to make successive sub-arrays the largest product

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.