|
| 1 | +package interview; |
| 2 | + |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class bk4 { |
| 6 | + public static void main(String[] args) { |
| 7 | + //int[] arr = new int[]{1,4,3,2,5}; |
| 8 | + int[] arr = new int[]{1,2,3,4,5,8}; |
| 9 | + //int[] arr = new int[]{8,7,6,5,4,3}; |
| 10 | + System.out.println(func(arr)); |
| 11 | + } |
| 12 | + public static int func(int[] arr){ |
| 13 | + int res = Integer.MAX_VALUE; |
| 14 | + for (int i = 0; i < arr.length; i++) { |
| 15 | + int cur = 0; |
| 16 | + int max1 = -1; |
| 17 | + int j = 0; |
| 18 | + for (; j < i; j++) { |
| 19 | + max1 = Math.max(max1+1, j>0?arr[j-1]:0); |
| 20 | + cur += Math.max(max1 - arr[j]+1, 0); |
| 21 | + } |
| 22 | + max1 = Math.max(max1, j>0?arr[j-1]:0); |
| 23 | + int max2 = -1; |
| 24 | + j = arr.length-1; |
| 25 | + for (; j>i ; j--) { |
| 26 | + max2 = Math.max(max2+1, j<arr.length-1?arr[j+1]:0); |
| 27 | + cur += Math.max(max2 - arr[j] + 1, 0); |
| 28 | + } |
| 29 | + max2 = Math.max(max2, j<arr.length-1?arr[j+1]:0); |
| 30 | + cur += Math.max(Math.max(max1, max2) + 1 - arr[i], 0); |
| 31 | + res = Math.min(res, cur); |
| 32 | + } |
| 33 | + return res; |
| 34 | + } |
| 35 | + |
| 36 | + public static void main2(String[] args) { |
| 37 | + Scanner scanner = new Scanner(System.in); |
| 38 | + int n = scanner.nextInt(); |
| 39 | + int [] nums = new int [n]; |
| 40 | + for(int i = 0; i < n; i ++) { |
| 41 | + nums[i] = scanner.nextInt(); |
| 42 | + } |
| 43 | + int [] temp = new int [n]; |
| 44 | + for(int i = 0; i < n; i ++) { |
| 45 | + temp[i] = nums[i]; |
| 46 | + } |
| 47 | + int count = Integer.MAX_VALUE; |
| 48 | + int sum = 0; |
| 49 | + for(int i = 1; i < n; i ++) { |
| 50 | + if(nums[i] <= nums[i - 1]) { |
| 51 | + sum += nums[i - 1] + 1 - nums[i]; |
| 52 | + nums[i] = nums[i - 1] + 1; |
| 53 | + } |
| 54 | + } |
| 55 | + if(sum < count) |
| 56 | + count = sum; |
| 57 | + for(int i = 0; i < n; i ++) { |
| 58 | + nums[i] = temp[i]; |
| 59 | + } |
| 60 | + sum = 0; |
| 61 | + for(int j = n - 2; j >= 0 ; j --) { |
| 62 | + if(nums[j] <= nums[j + 1]) { |
| 63 | + sum += nums[j + 1] + 1 - nums[j]; |
| 64 | + nums[j] = nums[j + 1] + 1; |
| 65 | + } |
| 66 | + } |
| 67 | + if(sum < count) |
| 68 | + count = sum; |
| 69 | + for(int i = 0; i < n; i ++) { |
| 70 | + nums[i] = temp[i]; |
| 71 | + } |
| 72 | + for(int i = 1; i < n - 1; i ++) { |
| 73 | + sum = 0; |
| 74 | + int j = 1; |
| 75 | + for(; j < i; j ++) { |
| 76 | + if(nums[j] <= nums[j - 1]) { |
| 77 | + sum += nums[j - 1] + 1 - nums[j]; |
| 78 | + nums[j] = nums[j - 1] + 1; |
| 79 | + } |
| 80 | + } |
| 81 | + j --; |
| 82 | + int k = n - 2; |
| 83 | + for(; k > i; k--) { |
| 84 | + if(nums[k] <= nums[k + 1]) { |
| 85 | + sum += nums[k + 1] + 1 - nums[k]; |
| 86 | + nums[k] = nums[k + 1] + 1; |
| 87 | + } |
| 88 | + } |
| 89 | + k ++; |
| 90 | + int res = Math.max(nums[j], nums[k]); |
| 91 | + if(nums[i] <= res) { |
| 92 | + sum += res + 1 - nums[i]; |
| 93 | + } |
| 94 | + if(sum < count) |
| 95 | + count = sum; |
| 96 | + for(int l = 0; l < n; l ++) { |
| 97 | + nums[l] = temp[l]; |
| 98 | + } |
| 99 | + } |
| 100 | + System.out.println(count); |
| 101 | + } |
| 102 | +} |
0 commit comments