Untitled
unknown
plain_text
3 years ago
937 B
8
Indexable
import java.util.ArrayList;
import java.util.List;
class Result {
public static int reductionCost(List<Integer> num) {
int a[] = num.stream().mapToInt(Integer::intValue).toArray();
int i = 0;
int j = a.length - 1;
return minCost(a, i, j);
}
private static int minCost(int a[], int i,
int j)
{
if (i >= j) {
return 0;
}
int best_cost = Integer.MAX_VALUE;
for (int pos = i; pos < j; pos++) {
int left = minCost(a, i, pos);
int right = minCost(a, pos + 1, j);
best_cost = Math.min(
best_cost,
left + right + combine(a, i, j));
}
return best_cost;
}
private static int combine(int a[], int i, int j)
{
int sum = 0;
for (int l = i; l <= j; l++)
sum += a[l];
return sum;
}
}Editor is loading...