最大可整合子数组长度
先给出可整合数组的定义:如果一个数组在排序之后,每相邻两个数的差的绝对值都为1,或者该数组长度为1,则该数组为可整合数组。
例如,[5, 3, 4, 6, 2]排序后为[2, 3, 4, 5, 6],符合每相邻两个数差的绝对值都为1,所以这个数组为可整合数组
给定一个数组arr, 请返回其中最大可整合子数组的长度。例如,[5, 5, 3, 2, 6, 4, 3]的最大可整合子数组为[5, 3, 2, 6, 4],所以请返回5
注意:本题中子数组的定义是数组中连续的一段区间,例如 [1,2,3,4,5] 中 [2,3,4] 是子数组,[2,4,5] 和 [1,3] 不是子数组
不能排序:注意:本题中子数组的定义是数组中连续的一段区间
eg:
暴力找到到每个子数组
以上是暴力方法,优化:满足以下两个条件必可整合
如果定义很长很复杂,看能不能改验证标准
public static int getLIL(int[] arr) { if (arr == null || arr.length == 0) { return 0; } int len = 0; int max = 0; int min = 0; HashSet<Integer> set = new HashSet<Integer>(); for (int L = 0; L < arr.length; L++) { // L 左边界 set.clear(); max = Integer.MIN_VALUE; min = Integer.MAX_VALUE; for (int R = L; R < arr.length; R++) { // R 右边界 // arr[L..R]这个子数组在验证 if (set.contains(arr[R])) { // arr[L..R]上开始 出现重复值了,arr[L..R往后]不需要验证了, // 一定不是可整合的 break; } // arr[L..R]上无重复值 set.add(arr[R]); max = Math.max(max, arr[R]); min = Math.min(min, arr[R]); if (max - min == R - L) { // L..R 是可整合的 len = Math.max(len, R - L + 1); } } } return len; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] arr = new int[N]; for(int i = 0 ; i < N ; i++) { arr[i] = sc.nextInt(); } System.out.println(getLIL(arr)); }