30 contiguous sub-arrays of the most Yamato

Source: Internet
Author: User

Topic description Hz Occasionally take some professional questions to confuse those non-computer majors. Today, the test team after the meeting, he said again: in the Ancient one-dimensional pattern recognition, it is often necessary to calculate the maximum sum of continuous sub-vectors, when the vector is all positive, the problem is well solved. However, if the vector contains a negative number, should it contain a negative number and expect that the next positive number will compensate for it? For example: {6,-3,-2,7,-15,1,2,2}, the maximum and 8 of continuous sub-vectors (starting from No. 0 to 3rd). Will you be fooled by him? (The length of the sub-vector is at least 1)
1  Public classSolution {2      Public intFindgreatestsumofsubarray (int[] a) {3         intSum =a[0],max=a[0] ;4          for(inti=1;i<a.length;i++){5             if(sum<=0)6sum=A[i]; 7             Else8sum+=A[i]; 9             if(Sum>max) max=sum;Ten          } One          returnMax; A     } -}

30 contiguous sub-arrays of the most Yamato

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.