Maximum sub-array and (maximum sub-segment and)

Source: Internet
Author: User

For example, for an array [1,-, 5,-], the maximum subarray and sum [,-] = 9, we need the function to output the maximum value of the subarray and, return the left and right boundary of the sub-array (left and right parameters of the following function ). this article stipulates that when all the numbers in the array are less than 0, the maximum number in the array is returned (you can also specify that 0 is returned, as long as maxsum in the following code is initialized to 0, at this time, we should pay attention to the-1 0 0 0-2 situation, especially if you want to output the starting position of the sub-array, if it is an interview, you should ask the interviewer) the following code is available at PAT 1007. maximum Subsequence Sum passed the test. The main function is tested as follows: int main () {int n; scanf ("% d", & n); vector <int> vec (n ); for (int I = 0; I <n; I ++) scanf ("% d", & vec [I]); int left, right; int maxs Um = maxSum1 (vec, left, right); // Replace the function name if (maxsum> = 0) printf ("% d", maxsum, vec [left], vec [right]); else printf ("0% d % d", vec [0], vec [n-1]);} refer: the beauty of programming 2.14 is the maximum Algorithm for Finding the sum of arrays sub-arrays 1: The simplest is to enumerate all sub-arrays and then sum them. The complexity is O (n ^ 3) int maxSum1 (vector <int> & vec, int & left, int & right) {int maxsum = INT_MIN, sum = 0; for (int I = 0; I <vec. size (); I ++) for (int k = I; k <vec. size (); k ++) {sum = 0; for (int j = I; j <= K; j ++) sum + = vec [j]; if (sum> maxsum) {maxsum = sum; left = I; right = k ;}} return maxsum ;} algorithm 2: The third re-cycle of the above Code has done a lot of repetitive work, slightly improved as follows, the complexity is O (n ^ 2) int maxSum2 (vector <int> & vec, int & left, int & right) {int maxsum = INT_MIN, sum = 0; for (int I = 0; I <vec. size (); I ++) {sum = 0; for (int k = I; k <vec. size (); k ++) {sum + = vec [k]; if (sum> maxsum) {maxsum = sum; left = I; right = k ;}}} return maxsum;} algorithm 3: Divide and conquer method. The following is an explanation of the beauty of programming. The complexity is O (nlogn) image // calculates the maximum subarray and of the array vec [start, end, the maximum subarray boundary is [left, right] int maxSum3 (vector <int> & vec, const int start, const int end, int & left, int & right) {if (start = end) {left = start; right = left; return vec [start];} int middle = start + (end-start)> 1 ); int lleft, lright, rleft, rright; int maxLeft = maxSum3 (vec, start, middle, lleft, lright); // The largest portion of the left half and int maxRight = MaxSum3 (vec, middle + 1, end, rleft, rright); // the largest and int maxLeftBoeder = vec [middle], maxRightBorder = vec [middle + 1], mleft = middle, mright = middle + 1; int tmp = vec [middle]; for (int I = middle-1; I> = start; I --) {tmp + = vec [I]; if (tmp> maxLeftBoeder) {maxLeftBoeder = tmp; mleft = I ;}} tmp = vec [middle + 1]; for (int I = middle + 2; I <= end; I ++) {tmp + = vec [I]; if (tmp> maxRightBorder) {ma XRightBorder = tmp; mright = I ;}int res = max (maxLeft, maxRight), maxLeftBoeder + maxRightBorder); if (res = maxLeft) {left = lleft; right = lright;} else if (res = maxLeftBoeder + maxRightBorder) {left = mleft; right = mright;} else {left = rleft; right = rright;} return res ;} algorithm 4: dynamic planning. The array is vec []. Set dp [I] to the largest sum of subarrays ending with vec [I]. For element vec [I + 1], it has two options: a, vec [I + 1], followed by the first sub-array to form the largest and B, vec [I + 1] to form a sub-array. Dp [I + 1] = max {dp [I] + vec [I + 1], vec [I + 1, the following code is used: int maxSum _ (vector <int> & vec) {int maxsum = INT_MIN, sum = 0; for (int I = 0; I <vec. size (); I ++) {sum = max (sum + vec [I], vec [I]); maxsum = max (maxsum, sum) ;}return maxsum ;} change the code above and record the position of the maximum sub-array int maxSum4 (vector <int> & vec, int & left, int & right) {int maxsum = INT_MIN, sum = 0; int begin = 0; for (int I = 0; I <vec. size (); I + +) {If (sum> = 0) {sum + = vec [I];} else {sum = vec [I]; begin = I;} if (maxsum <sum) {maxsum = sum; left = begin; right = I ;}} return maxsum;} if the array is cyclic, in this case, there are two situations (the red box in the figure shows the obtained largest subarray, left and right are the beginning and end of the subarray respectively): (1) if the largest subarray does not span vec [n-1] To vec [0], this is the case of each loop. image (2) for example, the largest subarray spans vec [n-1] To vec [0] image. In the second case, it is equivalent to extracting a part from the original array (vec [right + 1],…, Vec [left-1]), we only need to make the extracted and minimum values. The minimum subarray is similar to the maximum subarray. The Code is as follows, the following code is used to test the maximum sub-array of the array first and foremost connected to oj1572 and outputs 0 if the array is all negative. int maxSumCycle (vector <int> & vec, int & left, int & right) {int maxsum = INT_MIN, curMaxSum = 0; int minsum = INT_MAX, curMinSum = 0; int sum = 0; int begin_max = 0, begin_min = 0; int minLeft, minRight; for (int I = 0; I <vec. size (); I ++) {sum + = vec [I]; if (curMaxSum> = 0) {curMaxSum + = vec [I];} else {curMaxSum = vec [I]; begin_max = I;} if (maxsum <curMaxSum) {maxsum = curMaxSum; left = begin_max; right = I ;} //////////// subarray with the smallest sum, if (curMinSum <= 0) {curMinSum + = vec [I];} else {curMinSum = vec [I]; begin_min = I ;}if (minsum> curMinSum) {minsum = curMinSum; minLeft = begin_min; minRight = I ;}} if (maxsum> = sum-minsum) return maxsum; else {left = minRight + 1; right = minLeft-1; return sum-minsum ;}}

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.