Maximum child columns and issues

Source: Internet
Author: User

Problem Description:GivenA sequence of k integers {N?1??,N?2??, ...,N?K?? }, "Continuous child column" is defined as {N?I??,N?I+1??, ...,< Span class= "Mord" >n ? j??  }, where  < Span class= "base textstyle uncramped" >1≤i≤j≤k. "Maximum child columns and" are defined as the and the largest of all contiguous child column elements. For example, given sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub-columns {11,-4, 13} have the largest and 20. You are now asked to write a program that calculates the maximum sub-columns of a given integer sequence.

/span> 1. Method of exhaustive: Time complexity O (n^3)

Exhaustive method, calculated all possibilities, Time complexity O (n^3).

1 intMAXSUBSEQSUM1 (intA[],intsize)2 {3     intthissum,maxsum=0;4      for(inti =0; I < size;i++)5          for(intj = I;j < size;j++)6         {    7Thissum =0;8              for(intK = i;k <= j;k++)9Thissum + =A[k];Ten             if(Maxsum <thissum) OneMaxsum =thissum; A         } -     returnmaxsum; -}

2. Algorithm two: Time complexity O (n^2)

On the basis of the poor lifting method, slightly improved, less a for loop, the efficiency has improved, the code is easy to understand.

intMAXSUBSEQSUM2 (intA[],intsize) {    intthissum,maxsum=0;  for(inti =0; I < size;i++) {thissum=0;  for(intj = I;j < size;j++) {thissum+=A[j]; if(Maxsum <thissum) Maxsum=thissum; }        }    returnmaxsum;}

3. Divide and Conquer: Time Complexity O (NLOGN)

Adopt the idea of recursion. Maximum child column and there are three cases, the left largest child columns, the right largest child columns, across the middle of the largest sub-columns, take three of the largest. Recursion to the last namely, two numbers, comparing the left number of the right number or their and, fetching large.

1 //Divide and conquer2 intMAXSUBSEQSUM3 (intA[],intsize)3 {4     returnMaxsum (A,0, size-1);5 }6 7 intMaxsum (intA[],intLeftintRight )8 {9     if(left = =Right )Ten         returnA[left] >0? A[left]:0; One     intCenter = (left + right)/2;  A     intMaxleftsum =maxsum (a,left,center); -     intMaxrightsum = Maxsum (a,center+1, right); -     //find the left boundary maximum value the     intCenterto_left =0, Maxsum_centerto_left =0; -      for(inti = Center;i >= left;i--){ -Centerto_left + =A[i]; -         if(Maxsum_centerto_left <centerto_left) +Maxsum_centerto_left =Centerto_left; -     } +     //ask for the right boundary maximum value A     intCenterto_right =0, Maxsum_centerto_right =0; at      for(inti = center+1; I <= right;i++){ -Centerto_right + =A[i]; -         if(Maxsum_centerto_right <centerto_right) -Maxsum_centerto_right =Centerto_right; -     } -     returnMAXSUM3 (Maxleftsum,maxrightsum,maxsum_centerto_left +maxsum_centerto_right); in } - intMAXSUM3 (intAintBintc) to { +     return(A&GT;B?A:B) >c? (a>b?)a:b): C;  -}

4. Online Processing: Time complexity O (n)

The rule of online processing is to seek the child columns from the beginning and record the current maximum sub-columns, if the current sub-columns is negative, discard it. The code clearly illustrates this rule.

Personal understanding of online processing of the core: the current sub-column and if positive, it is always possible to make the following sub-column and become larger. If the current sub-columns is negative, it is not possible to be behind the child column and become larger, so discard.

1 //Online processing2 intMAXSUBSEQSUM4 (intA[],intsize)3 {4     intthissum=0, maxsum=0;5       for(inti =0; I < size;i++){6Thissum + =A[i];7         if(Maxsum <thissum)8Maxsum =thissum;9         Else if(Thissum <0)TenThissum =0; One     } A     returnmaxsum; -}

Organize lessons from Mooc Chen, He Chinming data structures

Maximum child columns and issues

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.