Cumulative and less than or equal to the length of the oldest array

Source: Internet
Author: User

Package Com.hzins.suanfa;/** * Cumulative and less than or equal to the length of the oldest array to be worth * @author Administrator **/ Public classDemo1 { Public Static intMaxLength (int[] arr,intk) {        int[] h =New int[Arr.length +1]; intsum =0; h[0] =sum;  for(inti =0; I!=arr.length;i + +) {sum+=Arr[i]; H[i+1] = H[i] +Math.max (sum, h[i]); } Sum=0; intres=0; intPre=0; intLen =0;  for(inti =0; I! = arr.length;i++) {sum+=Arr[i]; Pre= Getlessindex (h, Sum-k); Len= Pre = =-1?0: I-pre +1; Res=Math.max (res, Len); }        returnRes; }    /** * The position of the first number greater than or equal to I in "H" * @param h * @param i * @return*/    Private Static intGetlessindex (int[] H,inti) {intleft =0; intright = H.length-1; intMID =0; intres =-1;  while(Left <=Right ) {Mid= (left + right)/2; if(H[mid] >=i) { Right= Mid-1; Res=mid; }Else{ Left= Mid +1; }        }        returnRes; }     Public Static voidMain (string[] args) {int[] Array = {1,-2,3,-4,5,6,-6,7,-7,8}; System. out. println (MaxLength (Array,-1)); }}

Cumulative and less than or equal to the length of the oldest array

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.