Contiguous sub-arrays are the most large

Source: Internet
Author: User

Source: "Sword point offer" face question 31, "The beauty of programming" 2.14

Title: Enter an array of shapes with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array. To find the maximum value of all sub-arrays

Solution One: Assuming that the ID represents a starting point of the sequence, J represents the end point. If a[i] is negative, it cannot represent the starting point of the optimal subsequence, because any subsequence that contains a[i] as the starting point can be improved by a[i+1]. Similarly, any negative subsequence is unlikely to be the prefix of the optimal sub-sequence. For example, there is an array {1,-2,3,10,-4,7,2,-5}. We try to accumulate each number in the array one by one at the end of the head. Initialized and is 0. The first step plus the first number 1, at this time and 1. The next step is to add the number 2, and then it becomes the 1. The third step plus the number 3. We note that the sum of the previous cumulative and 1, less than 0, if used-1 plus 3, the sum is 2, smaller than the 3 itself. That is, the sub-array starting with the first number and the sum of the sub-arrays that begin with the third number. So we don't have to think about the sub-array that starts with the first number, the sum of the previous sums is discarded.

Time complexity O (n)

BOOLG_invalidinput =false;intGetmaxsum (intNumbersintLen) {    if(numbers = = NULL | | Len <=0) {G_invalidinput=true; return 0; } g_invalidinput=false; intMax_sum =int_min; intCurrent_sum =0;  for(inti =0; i < Len; i++) {        if(Current_sum <=0) {current_sum=Numbers[i]; } Else{current_sum+=Numbers[i]; }        if(Current_sum >max_sum) Max_sum=current_sum; }    returnmax_sum;}

Solution Two: The method of division and treatment. Suppose we are looking for the largest subarray of sub-array A[low,high]. The use of divide-and-conquer technology means that we want to divide the Subarray into two sub-arrays of the same scale as possible. That is, find the central location of the sub-array, such as mid, and then consider solving two sub-arrays A[low. Mid] and A[mid+1..high]. A[low. High] in any contiguous subarray of a[i: J] Location must be one of the following three situations:

1. Completely in sub-array a[low. Mid], so Low<=i<=j<=mid

2. Completely in sub-array A[mid+1..high], so Mid<i<=j<=high

3. Across the midpoint, so low<=i<=mid<j<=high.

Therefore, A[low. The position of one of the largest sub-arrays of high] must be one of these three cases. In fact, A[low. One of the largest sub-arrays of high] must be completely located in A[low. Mid], full in A[mid+1..high, or all sub-arrays that span the midpoint and the largest. We can solve a[low in a recursive way. Mid] and A[mid+1..high], because these two self-problems are still the largest sub-array problem, but smaller in size. Therefore, all that is left is to look for the largest subarray across the midpoint, and then select and the largest among the three.

Time complexity O (NLGN)

BOOLG_invalid_input =false;intMaxsubarray (intNumbersintLen) {    if(numbers = = NULL | | Len <=0) {G_invalid_input=true; return 0; } g_invalid_input=false; returnMaxsubarray (Numbers,0, Len-1);}intMaxsubarray (int*numbers,intStartintend) {    if(Start = =end) {        returnA[start]; }    intMID = start + (End-start)/2; intLeft_max_subarray =Maxsubarray (Numbers, start, mid); intRight_max_subarray = Maxsubarray (numbers, Mid +1, end); intCrossing_subarray =int_min; intLeft_sum =int_min; intCurrent_sum =int_min;  for(inti = mid; I >= start; i--) {current_sum+=Numbers[i]; if(Current_sum >left_sum) Left_sum=current_sum; }    intRight_sum =int_min; Current_sum=int_min;  for(inti = mid +1; I <= end; i++) {current_sum+=Numbers[i]; if(Current_sum >right_sum) Right_sum=current_sum; }    intCrossing_sum = Left_sum +right_sum; if(Left_max_subarray >= crossing_sum && left_max_subarray >=Right_max_subarray)returnLeft_max_subarray; Else if(Right_max_subarray >= crossing_sum && right_max_subarray >=crossing_sum)returnRight_max_subarrayElse        returncrossing_sum;}

Resources:

1. "Sword Point offer" electronic industry press

2. The beauty of programming electronics Press

3. Introduction to Algorithms 4.1 Maximum sub-array problems mechanical industry press

4. "Data structure and algorithm analysis C + + description" (third edition) P42 people post and Telecommunications press

Contiguous sub-arrays are the most large

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.