Maximum sub-array problem-Chapter 8

Source: Internet
Author: User

Problem: given the array a [n], the greatest sum of its sub-array is required. For example, the input array is:

1,-2, 3, 10,-4, 7, 2,-5

And the largest sub-array is 3, 10,-4, 7, 2, so the output is 18.

I didn't think of anything more clever.AlgorithmPreviously, the so-called "brute force" algorithm can be used to solve the problem:

1. Simple algorithms:

Find the sum of each sub-array and the largest record.

 
Void maxsum_1 (int A [], int N) {int sum = Nm, M = Nm; // nm is a small negative number, such as-99999999. for (INT I = 0; I <n; I ++) {sum = 0; For (Int J = I; j <n; j ++) {sum + = A [J]; M = max (M, sum) ;}} cout <__function __< ":" <m <Endl ;}

The complexity of the algorithm is O (n2). The algorithm in the book also has an O (N3) algorithm. The complexity of this algorithm is that it starts from the beginning when sum is obtained, and will not be mentioned here.

2. Divide and conquer Law

Divide the array into two equal segments of L and U. The solution to the original problem is that the maximum sub-array is either in l, in U, or across L and U.

It is difficult to understand the situation of crossing the middle point: At this time, the left side is not to find the largest subarray, but to find the largest sum from the middle to the two sides.

Int maxsum_2_helper (int A [], int L, int U) {If (L> U) return nm; // invalid parameter if (L = U) return a [l]; // only one element int M = (L + u)/2; // calculate the maximum continuous half of the left side and INT Lmax = Nm; for (INT I = m, S = 0; I> = L; I --) // from the center to the leftmost end {S + = A [I]; lmax = max (Lmax, S);} // calculate the maximum continuous sum of half of the right and INT rmax = Nm; For (INT I = m + 1, s = 0; I <= u; I ++) {S + = A [I]; rmax = max (rmax, S );} /// merge result // rlm is the maximum sub-array in the left and right sub-arrays and INT rlm = max (maxsum_2_helper (A, l, m), maxsum_2_helper (, m + 1, u); Return max (rlm, Lmax + rmax); // merge the largest sum between the vertex and the two sides} void maxsum_2 (int A [], int N) {int M = maxsum_2_helper (A, 0, n-1); cout <__function __< ":" <m <Endl ;}

This algorithm is very delicate, and its complexity is O (nlogn ).


3. Clever scanning Algorithms

Consider if we have obtained a [0... The maximum sub-array of I-1] is m, then a [0... The maximum sub-array of I] is either 0 ~ In I-1 (stored in maxsofar), either as of I (stored in maxendinghere ).

 
Void maxsum_3 (int A [], int N) {int maxsofar = Nm, maxendinghere = Nm; For (INT I = 0; I <n; I ++) {maxendinghere = max (maxendinghere + A [I], a [I]); maxsofar = max (maxsofar, maxendinghere);} cout <__function __< ": "<maxsofar <Endl ;}

Another form of this algorithm:

 
Void maxsum_3_2 (int A [], int N) {int M = Nm, sum = 0; For (INT I = 0; I <n; I ++) {sum + = A [I]; M = max (M, sum); If (sum <0) sum = 0 ;}cout <__function __< ": "<m <Endl ;}

Related questions:

A) Find the continuous sub-array whose sum is closest to 0. In more general cases, find the continuous sub-array closest to the given real number t.

If a [I... J] the nearest 0, then a [0... The sum of I] is equivalent to a [0... J] and closest. So use the accumulate array. Create a new array s [N] from the original array (or directly operate on the original array), so that s [I] = sum (A [0... I]). Then sort s [N] and find the two nearest numbers in S [N. The difference between the two numbers is the sub-array nearest 0. Note that you need to create a new data structure to record the location of each element in the original array.

Since the fastest sorting is O (nlogn), the time complexity O (nlogn) and space O (n ).

CodeUse the sorting function sort of STL as follows.

Struct AP {int value; int POS ;}; bool operator <(const AP & N, const AP & M) {return n. value <m. value;} template <typename T> inline t ABS (t n) {return n <0? (-N): N;} int nearzero (int A [], int N) {AP * B = new AP [N] (); int sum = 0; for (INT I = 0; I <n; I ++) {B [I]. pos = I; sum + = A [I]; B [I]. value = sum;} Sort (B, B + n); // search for the two elements with the smallest absolute difference: int dmin = Nm; int T = 0; For (INT I = 1; I <n; I ++) {If (ABS (Dmin)> ABS (B [I]. value-B [I-1]. value) {dmin = ABS (B [I]. value-B [I-1]. value); t = I ;}/// the difference between T and the T-1 position element is very close to 0 int r = Nm; int S, E; /// S is the starting table of the sub-array, and E is the following table at the end of the sub-array if (B [T]. pos <B [T-1]. pos) {r = B [T-1]. value-B [T]. value; S = B [T]. pos + 1; E = B [T-1]. pos;} else {r = B [T]. value-B [T-1]. value; S = B [T-1]. pos + 1; E = B [T]. pos;} cout <__function __< ":" <r <Endl; cout <"[" <S <", "<e <"] "<Endl; Delete [] B; return r ;}

 I haven't thought of a proper solution to the problem of any real number yet. At first, I thought it was a feasible solution to translate each element in the array, but I did not find it feasible after careful analysis.

The largest submatrix problem is also found in the descending order.

To be continued.

Reference: http://www.cnblogs.com/wuyuegb2312/p/3139925.html

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.