4.2号美团笔试第二题完美数组
public static int peek(int n,int[] nums) { int[] left = new int[n]; int[] right = new int[n]; int[] t = Arrays.copyOf(nums,n); int ans = Integer.MAX_VALUE; for (int i = 1; i < n; i++){ if (nums[i - 1] + 1 > nums[i]){ left[i] = left[i - 1] + nums[i - 1] + 1 - nums[i]; nums[i] = nums[i - 1] + 1; } else{ left[i] = left[i - 1]; } } for (int i = n - 2; i >= 0; i--){ if (t[i + 1] + 1 > t[i]){ right[i] = right[i + 1] + t[i + 1] + 1 - t[i]; t[i] = t[i + 1] + 1; } else{ right[i] = right[i + 1]; } } for (int i = 0; i < n; i++) ans = Math.min(Math.max(left[i],right[i]), ans); return ans; }
#实习##美团##笔试题目##笔经#