The maximum subsequence and complexity of the array are O (nlgn) and O (n) algorithms. The former can find the position of the sequence.

Source: Internet
Author: User

 

Algorithm 1:

There is a relatively complex O (nlogn) solution for this problem, that is, recursion. If we find the position of the sequence, this will be the best algorithm (because we will have an O (n) algorithm later, but we cannot find the position of the largest subsequence ). In this method, we use divide-and-conquer ).

In our example, the largest subsequence may appear in three places, or in the left half, or in the right half, or across the middle of the input data, occupying the Left and Right Parts. In the first two cases, recursive solutions are provided. In the third case, the largest sum is obtained, and the largest sum of the first half is obtained (including the last element of the first half) and the largest sum of the second half (the first element that contains the second half.

// Recursive method, complexity is O (nlogn)

Long maxsumrec (const vector <int> & A, int left, int right)

{

If (Left = right)

{

If (A [left]> 0)

Return a [left];

Else

Return 0;

}

Int center = (left + right)/2;

Long maxleftsum = maxsumrec (A, left, center );

Long maxrightsum = maxsumrec (A, center + 1, right );

// Obtain the maximum value of the sequence ending with the last digit on the left.

Long maxleftbordersum = 0, leftbordersum = 0;

For (INT I = center; I> = left; I --)

{

Leftbordersum + = A [I];

If (leftbordersum> maxleftbordersum)

Maxleftbordersum = leftbordersum;

}

// Obtain the maximum value of the sequence ending with the last digit on the right.

Long maxrightbordersum = 0, rightbordersum = 0;

For (Int J = center + 1; j <= right; j ++)

{

Rightbordersum + = A [J];

If (rightbordersum> maxrightbordersum)

Maxrightbordersum = rightbordersum;

}

Return max3 (maxleftsum, maxrightsum,

Maxleftbordersum + maxrightbordersum );

}

Long maxsubsum1 (const vector <int> &)

{

Return maxsumrec (A, 0, A. Size ()-1 );

}

In addition, max3 (long, long, long) indicates to calculate the maximum value of the three long:

// Obtain the maximum value among the three long strings.

Long max3 (long a, long B, long C)

{

If (A <B)

{

A = B;

}

If (A> C)

Return;

Else

Return C;

}

Analyze this algorithm:

T (1) = 1

T (n) = 2 T (n/2) + O (N)

Finally, the complexity of the algorithm is as follows:O (nlogn).

Algorithm 2:

Next we will introduce a linear algorithm, which is typical of many smart algorithms: the running time is obvious, but the correctness is not obvious (not easy to understand ).

// Linear algorithm O (N)

Long maxsubsum2 (const vector <int> &)

{

Long maxsum = 0, thissum = 0;

For (Int J = 0; j <A. Size (); j ++)

{

Thissum + = A [J];

If (thissum> maxsum)

Maxsum = thissum;

Else if (thissum <0)

Thissum = 0;

}

Return maxsum;

}

It is easy to understand the time field.O (N)Yes, but it is hard to figure out why it is correct. In fact, this is an improvement of algorithm 2. During analysis, I represents the starting point of the current sequence, and J represents the end point of the current sequence. If we don't need to know the location of the best sub-sequence, I can be optimized.

One important idea is that if a [I] is a negative number, it cannot represent the starting point of the most sequential order, any subsequence containing a [I] as the starting point can be improved by using a [I + 1] as the starting point. Similarly, any negative subsequence cannot be the prefix of the optimal subsequence. For example, if we find that the subsequence from a [I] to a [J] is a negative number in a loop, we can promote I.The key conclusion is that we can not only push I to I + 1, but also push it to J + 1.

For example, P is any subscript between I + 1 and J... + A [J] is a negative number, then, any subsequence starting with subscript P will not be greater than the subsequence corresponding to subscript I and include the subsequence from a [I] to P-1. ). Therefore, it is safe to push I to J + 1 without missing the optimal solution.Note: although, if there is a sequence ending with a [J] and a negative number, it indicates that any number in the sequence cannot start with the largest subsequence formed by the number following a [J ]., however, it does not indicate that a sequence before a [J] is not the largest sequence,That is to say, it cannot be determined whether the maximum subsequence is before a [J] or after a [J], that is, the maximum subsequence position cannot be obtained. However, we can ensure that the maxsum value is the largest subsequence sum currently.This algorithm only scans data once. Once a [J] is read and processed, it does not need to be memorized. It isOnline algorithms.

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.