Methods for Solving the maximum field sum

Source: Internet
Author: User

Problem definition:

A sequence composed of N integers (which may be negative): A [1], a [2], a [3],…, A [n], for example, a [I] + A [I + 1] +... + The Sub-segment and maximum value of a [J] (0 <I <j <n ). When the given integer is negative, the sub-segment is defined as 0. Therefore, for example, the maximum sub-sequence of {5,-, 2} is {5, 2}, and its sum is 8, reaching the maximum; while the maximum subsequence of {5,-6, 4, 2} is {4, 2}, and its sum is 6.

Method 1:LearnedProgramThe design will, that is, enumerate I and j, and calculate the maximum value of the sum between I and a [I] to a [J.

Int maxsub (int * a, int N)

{

Int I, J, K, maxn = 0;

For (I = 0; I <n; I ++)

{

For (j = I + 1; j <n; j ++)

{

Int temp_max = 0;

For (k = I; k <= J; k ++)

{

Temp_max + = A [k];

}

If (temmax> maxn)

{

Maxn = temp_max;

}

}

}

Return maxn;

}

Time complexity O (N ^ 3 ). This is obviously unacceptable. In fact, a large number of repeated computations are carried out.

 

Method 2:

The field and result line can be calculated and stored in the S [] array, that is, preprocessing.

Int sum = 0.s[ N];

For (I = 0; I <n; I ++)

{

Sum + = A [I];

S [I] = sum;

}

In this way, the sum of the numbers between a [I] And a [J] is equal to s [J]-s [I]. So the time complexity is changed to O (N ^ 2). Is it better? Can it be optimized? Obviously, the optimization is O (n * logn) and O (n!

 

Method 3:

Consider whether O (N * logn) is available.AlgorithmWhat about it? Of course ......

If the given sequence a [1 .. n] into two equal segments A [1 .. n/2] And a [n/2 + 1: N], respectively. The maximum field of the given sequence has three types of condition rows:

1) And a [1. n/2.

2) Is the same as the maximum field of a [n/2 + 1: N.

3) The largest field and two parts are included. The other part is in a [n/2 + 1. N.

In the first two cases, we can use recursive methods. In the third case, we can calculate the maximum fields and values of the two parts and then add them together (Note: A [1 .. n/2] This section calculates the maximum field and ends with a [n/2], a [n/2 + 1 .. n] This section calculates the maximum field and starts with a [n/2 + 1 ). The maximum field of the sequence and the maximum value in these three cases.

Int maxsubitem (int * a, int low, int high)

{

Int S1, S2, s31, s32, I, j;

Int sum;

Int mid = (low + high)/2;

If (Low = high)

Return a [low];

Else

{

S1 = maxsubitem (A, low, mid );

S2 = maxsubitem (A, Mid + 1, high );

I = mid;

S31 = A [Mid];

While (s31 + A [I-1]> s31) & (I> LOW ))

{

S31 + = A [I-1];

I --;

}

J = Mid + 1;

S32 = A [Mid + 1];

While (s32 + A [J + 1]> s32) & (j

{

S32 + = A [J + 1];

J ++;

}

Sum = s31 + s32;

If (sum <S1) sum = S1;

If (sum <S2) sum = S2;

}

}

In this case, it is obvious that the time complexity is O (n * logn ). What if I had an O (n) algorithm? Actually, there is. This is naturally the idea of dynamic planning !!!

Method 4:

Int maxsub (int A, int N)

{

Int temp = 0, maxn =-INF, k = 1

Int start, end;

For (I = 1; I <= N; I ++)

{

Temp + = A [I];

If (temp> maxn)

{

Maxn = temp; Start = K; end = I;

}

If (temp <0)

{

Temp = 0; k = I + 1;

}

}

Return maxn;

}

Analyze this algorithm and borrow a temporary variable temp. There are three situations:

1. If temp> maxn, update maxn and save the start and end positions;

2. If temp <0, it means temp = 0, because temp <0 cannot continue to use temp to update the maximum value;

3. If 0 <temp <maxn, no operation is performed. This is a potential of temp and may be used to update subsequent values. In such a traversal, all the maximum values are found.

(The key to temp usage is to have a good understanding of this idea. It doesn't matter if you cannot understand it. This is a hard way to think about it .)

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.