对不起 我直接暴力了 O(n^3)

如果用滑动窗口感觉可以到 O(n^2)

import java.util.Scanner;
import java.util.ArrayList;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        ArrayList<Integer> items = new ArrayList<Integer>();
        while(in.hasNext()){
            items.add(in.nextInt());
        }

        Integer[] input = items.toArray(new Integer[0]);

        int max_cur = 0;
        int res = 0;

        for(int i=0;i<input.length;i++){
            for(int j =i+1;j< input.length;j++){
                int [] maxmin = find_maxmin(input,i,j);
                if(maxmin[0]-maxmin[1] == 1) {
                    max_cur = j-i+1;
                    res = Math.max(max_cur,res);
                }
            }
        }

        System.out.println(res);
    }


    public static int[] find_maxmin(Integer[] input, int l, int r){
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int i=l;i<=r;i++){
            max = Math.max(max,input[i]);
            min = Math.min(min,input[i]);

        }
        return new int[]{max,min};
    }
}