[LintCode] Minimum Adjustment Cost
2015-11-13 12:12
169 查看
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]|
Example
Given
[1,4,2,3]and target =
1, one of the solutions is
[2,3,2,3], the adjustment cost is
2and it's minimal.
Return
2.
Note
You can assume each number in the array is a positive integer and not greater than
100.
SOLUTION 1:
这个题幸好是可以认为array里的数是不大于100的正整数,不然真不好做。
首先这题看起来问的是minimize the sum所以看起来应该相识DP。但是,DP又不好想转移方程,所以先用*记忆化搜索*去做这个。
也就DFS + memory来做:
先来开一个result存放A[index] 变到[0-100]需要的cost,这里面大部分是不需要的,只留下跟上一位数相差target之内的数字。然后开一个helper(){}递归,然后接口需要输入的是,A数组,变化后的A数组,target,result,位置index。然后DFS的helper大致就是这样:
dfs(){
B.set(index, i);
dif = i - A.get(i);
dif = dif + dfs(...index + 1..);
result[index][i] = dif;
B.set(index, A.get(index));
}
然后加一个memory,在helper里面,就是result,记录每一个位置,每次的dif。
最后输出的结果是所有result里面的最小值。
代码:
public class Solution { /** * @param A: An integer array. * @param target: An integer. */ //f(x,y) 把x转换到y距离为k之内的cost //f(x,y) = min of (f(x - 1,y') + |A[x-1] - y|) public int MinAdjustmentCost(ArrayList<Integer> A, int target) { if (A == null || A.size() == 0){ return 0; } int size = A.size(); int[][] dp = new int[size][101]; for (int i = 0; i < size; i++){ for (int j = 1; j <= 100; j++){ dp[i][j] = Integer.MAX_VALUE; if (i == 0){ dp[i][j] = Math.abs(j - A.get(i)); } else { for (int k = 1; k <= 100; k++){ if (Math.abs(j - k) > target){ continue; } int dif = Math.abs(A.get(i) - j) + dp[i - 1][k]; dp[i][j] = Math.min(dp[i][j], dif); } } } } int result = Integer.MAX_VALUE; for (int i = 1; i <= 100; i++){ result = Math.min(result, dp[size - 1][i]); } return result; } }
View Code
相关文章推荐
- 汉语拼音转换工具(Python 版)
- jquery事件触发例子
- redis学习资源汇总
- item 3: 理解decltype
- 在网页中添加分享到微信、QQ、微博
- Fresco正传(3):DraweeHierarchy分析
- 数据库基本问题1
- mysqld(服务器)命令行选项
- python字符串编码报错解决
- git remote rename
- Linux 1>/dev/null 2>&1的含义
- ThinkPHP去除url中的index.php
- dsym文件分析工具
- 设计模式 状态模式 以自动售货机为例
- android编程之XML文件解析方法详解(附源码)
- CSS3新属性(上菜中.....)
- 初始化选中某行
- EBS事务处理对应的财务借贷关系
- Nginx配置问题
- mysql-debug