The largest and most continuous sub-vectors in a one-dimensional vector

Source: Internet
Author: User

Q: A one-dimensional vector: arr[n] = {I1,i2,i3,......, in}, calculates the maximum and the number of its successive sub-vectors. (That is, the interception of successive paragraphs makes the elements and the largest, the element has a negative value; The sub-vector can be empty, i.e., and the minimum is 0)

A:

The initial idea was to be exhaustive, and the double loop would count all successive elements and

for [i,n) {    for[j,n) {        caculate sum (arr[j],arr[j]);    }   }

It works, but it looks stupid.

This assumes that the Intercept interval is arr[start, end] , then Arr[start, end+1] the maximum of the continuous sub-vector and can be determined by arr[start, end]?

If Arr[start, end] conforms to the condition of the sub-vector (assuming its maximum and Maxsofar) contains the last element, then Arr[start, end+1] only need to see (end+1) This position,

If Arr[start, end] Qualifying sub-vectors do not contain the last element,

Then Arr[start, End+1] The qualifying sub-vector is either arr[start, end], or the new sub-vector containing (end+1) this element (its maximum and that is, Arr[start,end] The maximum and maxending+arr[end+1] of the last element);

So the arr[n] has the following code:

#defineN 20#defineMAX (a) (a) > (b)? (a):(B)Static intMaxsofar=0; //Indicates the maximum and the continuous sub-vectors in the Arr[start, end] Static intmaxending=0; // represents the maximum and the (end) element of the Arr[start, end]  /*static int start =-1; * * The location of//start has not found a good way to determine Static intEnd =-1;
voidFindmax () {intI=0; for(; i<n; + +i) { maxending = MAX (maxending+arr[i], 0 ); // if maxending plus arr[end+1] in arr[start,end] is less than 0, then maxending in arr[start,end+1] is 0; Maxsofar = MAX (Maxending,maxsofar); Maxsofar in//arr[start,end+1] is the maximum value arr[start,end+1 in Maxsofar and maxending] in arr[start,end]; if(maxsofar==maxending) End=i; }}< Span style= "color: #800080;" >< Span style= "color: #800080;" >/ * When arr[n] = { -39,45,26,-18,-92,17,-4,68,72,-2,99,4,-67,142,55,-59,19,-41,-72,13}; The output is maxsofar=384,end=14; */

The above-mentioned implementation is linear, and the efficiency is much higher than the N2 level of the poor lifting.

The largest and most continuous sub-vectors in a one-dimensional vector

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.