由买买提看人间百态

topics

全部话题 - 话题: tempcost
(共0页)
l*******0
发帖数: 95
1
My solution should be correct. It prints the followings when you run the
code
min cost = 2
B = 1 2 2 3
public class MinAdjustment {
public int[] adjust(int[] A, int target) {
// state: cost[i][v] - the total cost of changing A[i] to v, where v
belongs to [0, max]
// init: cost[0][v] = |A[0] - v|;
// function: cost[i][v] = min(cost[i-1][v - target ... v + target])
+ |A[i] - v|
// where v, v - target and v + target all belong to [0,
max]
... 阅读全帖
m*****k
发帖数: 731
2
can be simplified:

//NEW LINE
int currentMinCost = 0;
for(int i = 1; i < A.length; i ++) {
currentMinCost = Integer.MAX_VALUE;
for(int v = 0; v < range; v ++) {
// calculate the min cost for changing A[i] to v
cost[i][v] = Integer.MAX_VALUE;
for(int diff = -1 * target; diff <= target; diff ++) {
int v1 = v + diff;
if(v1 < 0) continue;
... 阅读全帖
(共0页)