Given an integer array, adjust each integers so that the difference of every adjacent integers are not greater than a given number target.

If the array before adjustment is A, the array after adjustment is B, you should minimize the sum of |A[i]-B[i]|

Note: You can assume each number in the array is a positive integer and not greater than 100.

Example

Given A = [1,4,2,3] and target = 1, one of the solutions is [2,3,2,3], the adjustment cost is 2 and it's minimal.

Return 2.

分析:

首先,对于数组里的每个数,它最终的值不可能大于这个数组里最大的数(max)。所以,每个数的范围只能是从1到max. 如果第i个数取的值是j, 那么对于第i - 1个数,它能取的范围是不是只能是Math.max(1, j - target) 到 Math.min(j + target, max)。

如果用cost[i][j] 表示第i个数取p那个值时从第0个数到第i个数的total cost, 那么 cost[i][j] = Math.min(Math.abs(j - A.get(i)) + costs[i - 1][k]),  Math.max(1, j - target)  <= k <= Math.min(j + target, max) and j - A.get(i))

备注:最好自己创建一个二维costs表,自己安照下面的代码走一遍就明白了。

 public class Solution {
/**
* cnblogs.com/beiyeqingteng/
*/
public int MinAdjustmentCost(ArrayList<Integer> A, int target) {
if (A == null || A.size() == ) return ;
int max = getMax(A);
int[][] costs = new int[A.size()][max + ]; for (int i = ; i < costs.length; i++) {
for (int j = ; j <= max; j++) {
costs[i][j] = Integer.MAX_VALUE;
if (i == ) {
// for the first number in the array, we assume it ranges from 1 to max;
costs[i][j] = Math.abs(j - A.get(i));
} else {
// for the number A.get(i), if we change it to j, then the minimum total cost
// is decided by Math.abs(j - A.get(i)) + costs[i - 1][k], and the range of
// k is from Math.max(1, j - target) to Math.min(j + target, max)
for (int k = Math.max(, j - target); k <= Math.min(j + target, max); k++) {
costs[i][j] = Math.min(costs[i][j], Math.abs(j - A.get(i)) + costs[i - ][k]);
}
}
}
} int min = Integer.MAX_VALUE;
for (int i = ; i < costs[].length; i++) {
min = Math.min(min, costs[costs.length - ][i]);
}
return min;
} private int getMax(ArrayList<Integer> A) {
int max = A.get();
for (int i = ; i < A.size(); i++) {
max = Math.max(max, A.get(i));
}
return max;
}
}

转载请注明出处: cnblogs.com/beiyeqingteng/

Minimum Adjustment Cost的更多相关文章

  1. Lintcode: Minimum Adjustment Cost

    Given an integer array, adjust each integers so that the difference of every adjcent integers are no ...

  2. HDU 1385 Minimum Transport Cost (Dijstra 最短路)

    Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...

  3. Minimum Transport Cost(floyd+二维数组记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  4. HDU1385 Minimum Transport Cost (Floyd)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. hdu 1385 Minimum Transport Cost(floyd &amp;&amp; 记录路径)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  6. hdu 1385 Minimum Transport Cost (Floyd)

    Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  7. NSOJ Minimum Transport Cost

    These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...

  8. ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)

    题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...

  9. Minimum Transport Cost Floyd 输出最短路

    These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...

随机推荐

  1. mysql的Replication机制

    mysql的Replication机制 参考文档:http://www.doc88.com/p-186638485596.html Mysql的 Replication 是一个异步的复制过程. 从上图 ...

  2. HBase命令(二) -- 表操作

    创建表 hbase shell> create 'mytable','col1','col2' //建表语句 create '表名','列簇名','列簇名','列簇名' hbase shell& ...

  3. iwebshop二次开发(2)

    设置模板为mini模式,类似于JSP中的sitemesh public $layout='site_mini'; 登录模块实现 function wuliu_login() { //如果已经登录,就跳 ...

  4. Android Studio-开启Preview视图

    Preview视图会在切换"Design"和"Text"视图的时候自动显示,可在右侧工具栏开启: 今天无意中关闭了,找了半天,原来可以在这个地方再次开启:

  5. C#数字格式化

    格式规范的完整形式:{index [,width][:formatstring]} index是此格式程序引用的格式字符串之后的参数,从零开始计数:width(可选) 是要设置格式的字段的宽度,wid ...

  6. oracle 中的dual表简介与用法

    Dual表是每个数据库创建时默认生成的,该表仅有一列一行. 1)分析dual表执行,如下:

  7. django 初级(一) 配置与周边

    一.下载安装 从 https://www.djangoproject.com/download/ 下载最新的 django 版本,写本文时最新版本为 django 1.7,官方说只需要 python6 ...

  8. ASP.NET中gridview获取当前行的索引值

    在用GridView控件时,我们经常会碰到获取当前行的索引,通过索引进行许多操作.例如,可以获得当前行某一个控件元素:设置某一元素的值等等.下面结合实例介绍几种获得GridView当前行索引值的方法. ...

  9. 一段可以使用的 hibernate获得对象->action存入List->jsp页面用<s:iterator>迭代的代码

    SelectAction.java @SuppressWarnings("serial") @Component("selectAction") @Scope( ...

  10. DiscuzX 论坛首页 和 分 区设置版块横排

    在论坛看到很多新手站长在咨询怎么样才可以设置和Discuz! 官方论坛首页一个分区下面横排3个板块或者更多呢?如下图: 下面我一起来操作下: 论坛 后台 论坛 板块管理 分区 编辑 图一: 图二: 说 ...