Maximal and problem of continuous sub-arrays and their changes

Source: Internet
Author: User

Enter an array of integers with positive and negative numbers in the array. One or more consecutive integers in an array make up a sub-array. The maximum value for the and of all sub-arrays.
such as input {1,-2,3,10,-4,7,2,-5}, and the largest sub-array is {3,10,-4,7,2}, the output should be 18
This is the topic you see in the "Offer of Swords", which can be solved in the time complexity of O (n), and this problem can be a sub-problem of many more complex problems. So the record deepens the impression.
Analysis:
With the {1,-2,3,10,-4,7,2,-5} listed above, you can think of it from the beginning, and the number of items in the current polygon is negative, and you need to discard the previous one.
That is, starting from 1, the maximum recorded at this time is 1, and then traversing to-2, because 1+ ( -2) <0, so discard-2, and then continue to analyze backwards, 3>1, so update the maximum value is 3, and then continue to traverse backwards, 3+10=13, update the maximum value is 13,13+ (-4) = 9, Continue to traverse backwards ...
That is, two intermediate variables are stored, one is used to store the sum of the largest contiguous subarray traversing to the current item (maxsum), and the other is used to store the sum of the largest sub-arrays containing the current item (cursum), and if cursum>maxsum, update maxsum with Cursum , or continue traversing.
The code is as follows:

#include <stdlib.h>#include <stdio.h>#include <iostream>//Enter the correct identifiersBOOLF_validate=true;intGetgreatestofsubarray (int*datas,intLength) {if(datas==null| | length<=0) {f_validate=false;return 0; }intcursum=0;intmaxsum=0x80000000;//0x80000000 is the smallest negative integer     for(intI=0; i<length;i++) {//If current and less than equals 0, discard it and start accumulating from the current item in the array        if(cursum<=0) {Cursum=datas[i]; }Else//Otherwise current and need to add the current item in the array{Cursum+=datas[i]; }//each time the current and after each calculation need to update the maximum and        if(cursum>maxsum) Maxsum=cursum; }returnMaxsum;}intMain () {intdatas[]={1,-2,3,Ten,-4,7,2,-5};intMax;max=getgreatestofsubarray (Datas,sizeof(datas)/sizeof(datas[0]));STD::cout<<max<<STD:: Endl;return 0;}

did not notice before, later read the book found can use dynamic planning to analyze the problem:
Set F (i) to indicate the maximum and the number of sub-arrays ending with the i digit, then we need to ask for max{f (i)},0<=i

#include <stdlib.h>#include <stdio.h>#include <iostream>/* CountFrom first to Last, including First and LastThe difference between the most Yamato*/intGetgreatest (int *datas,intFirstint  Last);intGetgreatestsubofarray (int* Datas,int length){if(datas==null| |length<=0)return-1;intcursum=0;intmaxsum=0;//Divide, divide the array into two parts with a datum point, and then seek the maximum sum of two parts, then the total for(intI=1;i<length-2; i++) {Cursum=getgreatest (datas,0, i) +getgreatest (datas,i+1,length-1);if(cursum>maxsum)        {maxsum=cursum; }    }returnMaxsum;} /* CountFrom first to Last, including First and LastThe difference between the most Yamato*/intGetgreatest (int *datas,intFirstint  Last){if(datas==null| | first<0|| Last<0|| first>= Last)return-1;intcursum=0;intmaxsum=0; for(inti=first;i< Last; i++)//From First to LastLoop through, calculate the difference between two adjacent items, converted into a continuous array of the maximum and problem {if(cursum<=0)//If current and less than0, discard it {cursum=datas[i+1]-datas[i]; }ElseOtherwise, add the current item {cursum+=datas[i+1]-datas[i]; }if(cursum>maxsum)//update maximum value {maxsum=cursum; }    }returnMaxsum;}intMain () {intdatas[]={1,4,5,2,3,1};intMax=getgreatestsubofarray (datas,sizeof (datas)/sizeof (datas[0])); std::cout<<max<<std::endl;return 0;}

Maximal and problem of continuous sub-arrays and their changes

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.