Dynamic programming-the largest and most continuous sub-sequences

Source: Internet
Author: User

given the sequence of k integers {n1,n2,..., Nk}, any contiguous subsequence can be expressed as {Ni, ni+1, ..., Nj}, where 1 <= i <= j <= K. The maximal contiguous subsequence is the element and the largest of all successive sub-sequences, such as the given sequence {-2, 11,-4, 13,-5,-2}, whose maximum contiguous subsequence is {11,-4,13}, the maximum contiguous subsequence, and 20.

Note: For convenience, if all integers are negative, the maximum subsequence and is 0.

Algorithm one, the exhaustive method, finds all the sub-arrays, and then finds the and of the sub-array, and takes the maximum value in all the sub-arrays

/*o (n^3) Poor lift method * Disadvantage: Repeat accumulation, and maxsum comparison, each i->j in the middle of the total accumulated before compared with the maxsum * */public    static int MaxSubSequence1 (int[] array) {          int length=array.length;      int maxsum=0;      int thissum=0;      for (int i=0;i<length;i++) {for      (int j=i;j<length;j++) {      thissum=0;      for (int k=i;k<j;k++) {//I->j add up between the      thissum+=array[k];      if (thissum>maxsum) {      maxsum=thissum;            }}}} return maxsum;          }
Algorithm two, the first method of each i->j between each iteration, repeated calculation of a lot, you can take advantage of the computed sub-array and

/*o (n^2) Exhaustive method     * I->j between each summation and maxsum comparison     * */public    static int MaxSubSequence2 (int[] array) {        int Length=array.length;      int maxsum=0;      int thissum=0;      for (int i=0;i<length;i++) {     thissum=0;    for (int j=i;j<length;j++) {   thissum+=array[j];      if (thissum>maxsum) {  maxsum=thissum;}}       }       return maxsum;    }

algorithm three, dynamic programming, Initializes a maximum array of maxsum[n],Maxsum[i] is the maximum sum of the subarray that represents a[0...i] ending with a[i], then maxsum[i] is equal to a[0...i-1] maximum and plus a[i] in and A[i] compare Max, maxsum[i] = max {Maxsum[i-1] + a[i], A[i]}

  Dynamic programming, state equation maxsum[i] = max{maxsum[i-1] + a[i], a[i]}; Maxsum[i] indicates the largest and public    static int MaxSubSequence3 (int[] array) with the end of A[i] {         int length=array.length;    Int[] Maxsum=new int[length];    MAXSUM[0]=ARRAY[0];      for (int i=1;i<length;i++) {      Maxsum[i]=math.max (maxsum[i-1]+array[i], array[i]);      }      Find the maximum value in maxsum      int maxsum=integer.min_value;      for (int i=0;i<maxsum.length;i++) {      if (maxsum[i]>maxsum) {      maxsum=maxsum[i];      }      }         return maxsum;    }

Improvement of Algorithm three O (n)

     /* Simplified method Three, when the previous summation and thissum less than 0 is set 0, discard, greater than maxsum, assign the value to maxsum     * */public    static int MaxSubSequence4 (int[] Array) {        int length=array.length;      int maxsum=0;      int thissum=0;      for (int i=0;i<length;i++) {      thissum+=array[i];      if (thissum>maxsum) {      maxsum=thissum;      }      else if (thissum<0) {      thissum=0;      }      }        return maxsum;    }



Dynamic programming-the largest and most continuous sub-sequences

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.