给定一个有序数组arr,代表坐落在X轴上的点,给定一个正数K,
代表绳子的长度,返回绳子最多压中几个点

import java.util.Arrays;

public class Code_CordCoverMaxPoint {
    public static int maxPoint1(int[] arr, int L){

//        if (L == 0){
//            return 1;
//        }

        int left = 0;
        int right = 0;
        int maxPoint = Integer.MIN_VALUE;
        while(left<=right && right < arr.length-1){
            if((arr[right+1]-arr[left]) > L){
                maxPoint = Math.max(maxPoint,right-left+1);
                left++;
                if(left > right){
                    right++;
                }
            }else{
                right++;
            }
        }

        if((arr[right]-arr[left]) <= L) {
            maxPoint = Math.max(maxPoint, right - left + 1);
        }
        return maxPoint;
    }

    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;
    }

    // for test
    public static int test(int[] arr, int L) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            int pre = i - 1;
            while (pre >= 0 && arr[i] - arr[pre] <= L) {
                pre--;
            }
            max = Math.max(max, i - pre);
        }
        return max;
    }

    // for test
    public static int[] generateArray(int len, int max) {
        int[] ans = new int[(int) (Math.random() * len) + 1];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = (int) (Math.random() * max);
        }
        Arrays.sort(ans);
        return ans;
    }

    public static void main(String[] args) {
        int len = 100;
        int max = 1000;
        int testTime = 100000;
        System.out.println("测试开始");
        for (int i = 0; i < testTime; i++) {
            int L = (int) (Math.random() * max);
            int[] arr = generateArray(len, max);
            int ans1 = maxPoint1(arr, L);
            int ans2 = maxPoint2(arr, L);
            int ans3 = test(arr, L);
            if (ans1 != ans2 || ans2 != ans3) {
                System.out.println("oops!");
                System.out.println(L);
                System.out.println(ans1 +" "+ans2+" "+ans3);
                for (int j = 0; j < arr.length; j++) {
                    System.out.print(arr[j]+" ");
                }
                break;
            }
        }
        System.out.println("测试结束");
    }
}