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 ...
随机推荐
- Yii2 redis与cache
原文地址:http://www.myexception.cn/php/1974979.html composer require yiisoft/yii2-redis 安装后使用超简单,打开 comm ...
- Samus驱动中的Document条件
今天要说一个东西就是Samus驱动里的 Document 和他的一个子类 Op 在Samus驱动的增删改查方法中都有这类的参数传递.. 大致的使用方法是这样.. MongoU.Find<Per ...
- 解决子元素margin让父辈元素位置一起改变的问题
1.在父元素内添加内容,并且要在子元素块前面添加,后面添加内容无效. 内容可以是文字.图片甚至是空格,源代码里直接按空格无效,可以用占位符 2.让子元素或父元素浮动float:left. 缺点:在元 ...
- iOS开发 关于SEL的简单总结
SEL就是对方法的一种包装.包装的SEL类型数据它对应相应的方法地址,找到方法地址就可以调用方法.在内存中每个类的方法都存储在类对象中,每个方法都有一个与之对应的SEL类型的数据,根据一个SEL数据就 ...
- 使用/调用 函数的时候, 前面加不加 对象或 this?
这个问题, 其实没有细想: 应该是这样的: (想明白了, 就会少很多困惑, 会对语言的把握 会 更深入更透彻) 任何一门 语言, (如果你自己去设计一门语言...). 都要规定 一些 "关键 ...
- php开发工具之火狐浏览器插件
相信做开发的都有一种火狐情怀吧! 下面来介绍下一些自己在php开发工程中用到几个火狐浏览器插件. 1.[firebug]: 这个插件可以说是一个神奇,功能不用过对介绍. 2.[hostAdmin]: ...
- H5移动端知识点总结
H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解display:box的布局 理解flex布局 Flex布局兼容知识点总结 回到顶部 移动开 ...
- POJ 2411 Mondriaan's Dream
状压DP Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9938 Accepted: 575 ...
- poj3070 (斐波那契,矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9630 Accepted: 6839 Descrip ...
- VC++ 6.0使用定时器SetTimer;
背景: windows中使用VC++6.0制作了个交互界面向下位机定时发送数据及显示下位机上传的数据.定时发送则需要使用定时器. 本文只做记录如何调用,原理以后再深究. 正文: 首先,我生成的窗体类名 ...