The beauty of programming the maximum product of a 2.13--sub-array

Source: Internet
Author: User

Defines an array of integers of length N, allowing only multiplication, not division, to calculate the largest group in any combination of any number (N-1).

Ideas

Suddenly become ya black, quite not used to ~ ~ or ya black good-looking?ω?

The implementation is simple, but I would not think of such a train of thought ... It seems that apart from accumulating, don't think of a different way to improve the π__π.

1. Use space to change time, open the array s and T, respectively, to save the results of the previous and backward multiplication and the result of the forward connection. For each N-1 element combination P[i] that excludes the element I,

Have p[i]=s[i-1]*t[i+1], get all the p[i], the maximum value can be traversed once.

2. Assume that the product of n integers is P, and the p is positive and negative analysis:

1) p=0

The array contains at least one 0, assuming that outside of the 0, the product of the number of other N-1 is Q, and then according to Q's positive and negative discussion:

A. Q=0: The product of at least two 0,n-1 number of arrays can only be 0, return 0;

B. Q>0: Return q;

C. Q<0: Replace any value with 0 with 0 and return 0.

2) p>0

If there is a positive number in the array, the minimum positive number is removed, and if the array is all negative, the absolute maximum negative number is removed.

3) p<0

The negative negative is positive, n elements remove a negative number, the product to get a positive number, you need to remove a negative value of the smallest.

"Codes"

Law One:

intMaxmulti (intN[],intN) {    int*s=New int[n+1]; int*t=New int[n+1]; int*p=New int[n]; intremax=int_min; s[0]=1; T[n]=1;  for(intI=1; i<=n; i++) S[i]=s[i-1]*n[i-1];  for(inti=n-1; i>=0; i--) T[i]=t[i+1]*N[i];  for(intI=0; i<n; i++) {P[i]=s[i-1]*t[i+1]; if(p[i]>Remax) Remax=P[i]; }    Delete[] s; Delete[] t; Delete[] p; returnRemax;}

Law II:

intMAXMULTI1 (intN[],intN) {    intMulti=1;  for(intI=0; i<n; i++) Multi*=N[i]; if(multi==0){        intq=1; intCount=0;  for(intI=0; i<n; i++)            if(n[i]!=0) {Q*=N[i]; Count++; }        if(count<n-1)return 0; Else if((count==n-1) && (q>0))returnQ; Else if((count==n-1) && (q<0))return 0; }    Else if(multi>0){        intR=0; intJ;  for(intI=0; i<n; i++)            if(n[i]>R) {R=N[i]; J=i; }        if(r==0)             for(intI=0; i<n; i++)                if(ABS (N[i]) >R) {R=ABS (N[i]); J=i; }        intq=1;  for(intI=0; i<n&&i!=j; i++) Q*=N[i]; returnQ; }    Else if(multi<0){        intR=0; intJ;  for(intI=0; i<n; i++)            if(ABS (n[i]>R)) {R=ABS (N[i]); J=i; }        intq=1;  for(intI=0; i<n&&i!=j; i++) Q*=N[i]; returnQ; }}

Evaluation

The topic is simple, but the idea can't be thought out. The second method is lengthy.

The beauty of programming the maximum product of a 2.13--sub-array

Related Article

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.