-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArrayAdjustmentCost.java
43 lines (36 loc) · 1.25 KB
/
ArrayAdjustmentCost.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* https://www.techiedelight.com/determine-minimal-adjustment-cost-array/
*/
import java.util.Random; // for test cases
class ArrayAdjustment {
private static int minCostRec(int[] array, int target, int n) {
if (n < 0) {
return 0;
}
int minCost = Integer.MAX_VALUE;
int original = array[n];
for (int i = array[n + 1] - target; i <= array[n + 1] + target; ++i) {
array[n] = i;
int cost = minCostRec(array, target, n - 1);
minCost = Math.min(minCost, cost + Math.abs(original - i));
array[n] = original;
}
return minCost;
}
public static int minCostRec(int[] array, int target) {
if (array == null) {
throw new IllegalArgumentException();
}
return minCostRec(array, target, array.length - 2);
}
}
class Driver {
public static void main(String[] args) {
int target = 1;
int[] array = {1, 3, 0, 3};
System.out.println("min cost: " + ArrayAdjustment.minCostRec(array, target));
// int[] array2 = {55, 77, 52, 61, 39, 6, 25, 60, 49, 47};
// int target2 = 10;
// System.out.println("min cost: " + ArrayAdjustment.minCostRec(array2, target2));
}
}