Given an integer array, adjust each integers so that the difference of every adjcent 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 [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.

这道题要看出是背包问题,不容易,跟FB一面paint house很像,比那个难一点

定义res[i][j] 表示前 i个number with 最后一个number是j,这样的minimum adjusting cost

如果第i-1个数是j, 那么第i-2个数只能在[lowerRange, UpperRange]之间,lowerRange=Math.max(0, j-target), upperRange=Math.min(99, j+target),

这样的话,transfer function可以写成:

for (int p=lowerRange; p<= upperRange; p++) {

  res[i][j] = Math.min(res[i][j], res[i-1][p] + Math.abs(j-A.get(i-1)));

}

 public class Solution {
/**
* @param A: An integer array.
* @param target: An integer.
*/
public int MinAdjustmentCost(ArrayList<Integer> A, int target) {
// write your code here
int[][] res = new int[A.size()+1][100];
for (int j=0; j<=99; j++) {
res[0][j] = 0;
}
for (int i=1; i<=A.size(); i++) {
for (int j=0; j<=99; j++) {
res[i][j] = Integer.MAX_VALUE;
int lowerRange = Math.max(0, j-target);
int upperRange = Math.min(99, j+target);
for (int p=lowerRange; p<=upperRange; p++) {
res[i][j] = Math.min(res[i][j], res[i-1][p]+Math.abs(j-A.get(i-1)));
}
}
}
int result = Integer.MAX_VALUE;
for (int j=0; j<=99; j++) {
result = Math.min(result, res[A.size()][j]);
}
return result;
}
}

Lintcode: Minimum Adjustment Cost的更多相关文章

  1. Minimum Adjustment Cost

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

  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. Lintcode: Minimum Subarray 解题报告

    Minimum Subarray 原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/# Given an array of intege ...

随机推荐

  1. mysql线程缓存和表缓存

    一.线程缓存1.thread_cache_size定义了线程缓冲中的数量.每个缓存中的线程通常消耗256kb内存2.Threads_cached,可以看到已经建立的线程二.表缓存(table_cach ...

  2. A Realistic Evaluation of Memory Hardware Errors and Software System Susceptibility

    http://www.cs.rochester.edu/~kshen/papers/usenix2010-li.pdf Abstract Memory hardware reliability is ...

  3. Android TextView 显示不全的自动补齐方式

    TextView在Android开发中用到的地方应该是很多的.很多时候,TextView会有一行显示不全被截取或者会换行.之前我的解决办法比较笨拙,定死TextView的一行字数长度,最后一个以省略号 ...

  4. Python 虚拟环境Virtualenv

    本人也是Python爱好者,众所周知,Python扩展多,每次为了测试,安装各种各样的扩展,这样导致本地的Python环境非常混乱,就有人想到搞个隔离环境  和 本地环境没有关系,随时可以删除这个隔离 ...

  5. iframe框架里镶嵌页面;<marquee>:滚动效果;<mark>做标记;内联、内嵌、外联;选择器

    标签:①②③④⑤⑥⑦★ 框架: 一.frameset:(框架集) 1.如果使用框架集,当前页面不能有body 2.cols="300,*":左右拆分,左边宽300,右边宽剩余 3. ...

  6. 【Java 基础篇】【第六课】接口interface

    Java提供的这个interface的语法,目的就是将接口从类中剥离出来,构成独立的主体. 首先加入我们定义了这个杯子接口: interface Cup { void addWater(int w); ...

  7. [Stanford 2011] 知识点小结

    1.获得帮助:option+click /  option+double click 2.@property里的nonatomic,表示非原子性访问,atomic是obj-c里使用的一种线程保护技术, ...

  8. Segments---poj3304(判断直线与线段的位置关系)

    题目链接:http://poj.org/problem?id=3304 题意:给你n个线段,求是否有一条直线与所有的线段都相交,有Yes,没有No; 枚举所有的顶点作为直线的两点,然后判断这条直线是否 ...

  9. UIPickerView 循环滚动(一种假象)

    因为网上没有查到相关方法,故而采用一种假象的方法来实现,选项循环滚动 - (void)viewDidLoad { [super viewDidLoad]; /** UIPickerView 选择器 * ...

  10. Selenium2学习-016-WebUI自动化实战实例-014-Selenium 窗口选择

    在日常的 WebUI 自动化测试脚本编写过程中,经常需要打开新的页面,或者在多个打开的页面之间进行切换,以对页面元素进行相应的操作,以模拟用户的行为,实现 UI 的自动化测试.在过往的时间中,经常有初 ...