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