Minimum Adjustment Cost
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的更多相关文章
- Lintcode: Minimum Adjustment Cost
Given an integer array, adjust each integers so that the difference of every adjcent integers are no ...
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- Minimum Transport Cost(floyd+二维数组记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- HDU1385 Minimum Transport Cost (Floyd)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1385 Minimum Transport Cost(floyd && 记录路径)
Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1385 Minimum Transport Cost (Floyd)
Minimum Transport CostTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- NSOJ Minimum Transport Cost
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
- ZOJ 1456 Minimum Transport Cost(Floyd算法求解最短路径并输出最小字典序路径)
题目链接: https://vjudge.net/problem/ZOJ-1456 These are N cities in Spring country. Between each pair of ...
- Minimum Transport Cost Floyd 输出最短路
These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...
随机推荐
- Autofac.Integration.Owin
public static IAppBuilder UseAutofacMiddleware(this IAppBuilder app, ILifetimeScope container) { if ...
- PHP数组处理函数的使用array_reduce(二)
关于PHP数组操作函数更为细致的用法大家还可以参考PHP在线参考手册:http://php.net/manual/zh/index.php array_reduce — 用回调函数迭代地将数组简化为单 ...
- git高级命令
git reflog 显示所有branch的commit,包括commit和reset,以及已删除的commit.而git log只显示当前branch的commit,不包括已删除的commit gi ...
- Python开发【第六篇】:模块
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- thinkphp-3
有两种创建项目的方式: 一是用多个单入口文件, 一个入口文件对应着一个项目, 如前台/后台/会员中心等 二是用一个单入口, 创建项目分组 对于有多个入口文件的 情况, 配置文件的共享问题? 不管是前台 ...
- webpack构建与loaders
loaders 定义 先了解一下webpack,webpack是一个用于针对js文件的构建工具,在被构建的js文件中,我们可以使用require语句和webpack loader,如下: var cs ...
- JS,html压缩及混淆工具
现在已经出现了不少有自己特色的: JSMin Javascript compressor Packer Closure Compiler YUI Compressor Pretty Diff Java ...
- C#创建windows服务列表
转载自:http://www.cnblogs.com/sorex/archive/2012/05/16/2502001.html Windows Service这一块并不复杂,但是注意事项太多了,网上 ...
- Linux 中常见的命令行,持续更新
1.添加自己的环境变量 root@adonis:~# echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin r ...
- QT点击"X"按钮,调用closeEvent()函数来实现调用特定事件(附:粗略介绍QT的信号与槽的使用方法)
背景: QT在用户关闭窗口(直接点击"X"键)时,程序一般都需要做一些善后的事情,就我现在的程序来说,既关闭USB.如何实现? 正文: 首先,在对应窗体的".h" ...