//绳子最大能盖的数组节点
public static void main(String[] args) {
int[] arr = {1,4,7,9,60};
System.out.println(maxPoint2(arr,50));
}
public static int maxPoint(int[] arr, int L) {//L是绳子的长度
int res =1;
for(int i =0;i<arr.length;i++) {
int nearest = nearestIndex(arr,i,arr[i]-L);
res = Math.max(res,i-nearest + 1);//(当前局部数组长度-起始下标)线段中间的节点+1
}
return res;
}
//方法一
public static int nearestIndex(int[] arr,int R,int value) {//二分查出绳子盖的起始下标位置
int L =0;
int index = R;
while(L <= R) {
int mid = L+ ((R-L)>> 1);
if(arr[mid]>=value) {
index = mid;
R = mid - 1;
} else {
L = mid+1;
}
}
return index;
}
//方法二
public static int maxPoint2(int[]arr, int L) {
int left = 0;
int right = 0;
int N =arr.length;
int max =0;
while(left < N) {
while(right < N && arr[right] - arr[left] <= L) {//窗口可移动,最大覆盖
right++;
}
max = Math.max(max,right-(left++));
}
return max;
}
标签:arr,能盖,int,mid,public,right,static,大厂,节点
From: https://www.cnblogs.com/15078480385zyc/p/17658821.html