O (N) The maximum number of sub-array lengths less than or equal to K in the array
1, first introduce O (NLOGN) solution, although it seems that nothing related.
An array of arr, which requires the maximum number of sub-arrays, we can first find the maximum number of sub-arrays that meet the criteria at the end of each position, and then take the maximum size.
Mr. Helparr into auxiliary array, where helparr[i] represents the sum of all the numbers on arr[0~i].
For position I, as long as the first position J of the >= Sum-k is obtained in Helparr, J + 1 to I is the maximum subarray of conditions that satisfies the condition at the end of position I.
The question turns to: How to get an array in O (Logn), where the first occurrence is greater than the number of sum-k.
Turn it into a monotone non-meiosis group, because only the earliest position of a number is required, and then only the position of the number larger than that number is meaningful.
Give me a chestnut:
Helparr = {0 1 3 2 7 5}→newarr = {0 1 3 3 7 7}
It is obvious that the position of the first number K of newarr can be solved by the dichotomy method.
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] =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; } Public Static intGetlessindex (int[] arr,intnum) { intLow = 0; intHigh = Arr.length-1; intMID = 0; intres =-1; while(Low <=High ) {Mid= (low + high)/2; if(Arr[mid] >=num) {Res=mid; High= Mid-1; } Else{ Low= Mid + 1; } } returnRes; }
2, O (N) solution
Sir into an auxiliary array h, where h[i] is denoted with arr[i] to start to the right, the smallest summation and.
Ends[i] Represents the end position of the position I, its minimum accumulation and.
So, we start with the i = 0 position, judging the minimum sum of the current position and whether it is <= K. If the condition is met, proceed to settlement.
Give me a chestnut:
0 ... I i+1 .... J j+1 .... X x+1
The cumulative and <= K,i starting from 0 are positions where the minimum sum and end of the 0 position start. Next depends on whether the technology can go down. Need to see the situation of i+1.
If the summation and sum of the 0~i, plus the accumulation and <=k of the i+1, can be extended to J at a distance. Then it depends on the situation of j+1.
If the sum of the preceding sums plus the accumulation of j+1 and does not satisfy the condition, then terminates.
You can now settle the oldest array that satisfies the condition, starting with 0.
Then calculate the next position 1.
Public Static intGetmaxlen (int[] arr,intk) { int[] h =New int[Arr.length]; int[] ends =New int[Arr.length]; H[arr.length-1] = Arr[arr.length-1]; Ends[arr.length-1] = Arr.length-1; for(inti = arr.length-2; I >= 0; i--) {H[i]= H[i + 1] < 0? Arr[i] + h[i+1]: arr[i]; if(H[i+1] > 0) {H[i]=Arr[i]; Ends[i]=i; }Else{H[i]= Arr[i] + h[i + 1]; Ends[i]= ends[i + 1]; } } intend = 0;//Right Border intres = 0;//Window accumulation and intsum = 0; for(inti = 0; i < arr.length; i++){ while(End < Arr.length && sum + h[end] <=k) {Sum+=H[end]; End= Ends[end] + 1; } Sum-= end > I? Arr[i]: 0, //If End does not move at all, sum minus arr[i] means starting from the i+1 position. Sum does not need to be zeroed, because only a few values need to be updated to be reused. Res= Math.max (res, end-i); End= Math.max (end, i + 1); } returnRes; }
O (N) to find the maximum subarray length in the array that is less than or equal to K