Lintcode: Minimum Adjustment Cost
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的更多相关文章
- Minimum Adjustment Cost
Given an integer array, adjust each integers so that the difference of every adjacent integers are n ...
- 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 ...
- Lintcode: Minimum Subarray 解题报告
Minimum Subarray 原题链接: http://lintcode.com/zh-cn/problem/minimum-subarray/# Given an array of intege ...
随机推荐
- Nginx模块fastcgi_cache的几个注意点
fastcgi响应http请求的结果中,响应头包括Expires、Cache-Control、Set-Cookie三个,都会可能不被cache. thinkphp3.0禁止session自动启动 co ...
- 百度Ueditor编辑器的Html模式自动替换样式的解决方法
百度的Ueditor编辑器出于安全性考虑,用户在html模式下粘贴进去的html文档会自动被去除样式和转义.虽然安全的,但是非常不方便. 做一下修改把这个功能去掉. 一.打开ueditor.all.j ...
- javaWeb中servlet开发(1)——helloworld
1.servlet 1.1 servlet简介 1.2 servlet流程 不管是servlet还是jsp,所有的程序都是在服务器端处理的,所以必须了解一个servlet基本流程 servlet和JS ...
- P1970 花匠
状态定义是dp中非常重要的,可以直接影响到效率,如此题,第一种思路是: #include <bits/stdc++.h> using namespace std; const int ma ...
- 蓝牙 BLE GATT 剖析(二)-- GATT UUID and 举例
generic attribute profile (GATT)The Generic Attributes (GATT) define a hierarchical data structure t ...
- Java高级之内存模型分析
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 下文是博主感悟,请带着怀疑性的态度阅读! 需要了解基本变量所占 ...
- 【Android开发学习笔记】【随笔】UI线程
概念 UI线程 是Android中的主线程,涉及到UI方面的一些操作都需要在ui线程中进行操作 在非ui线程中想操作ui,就会报错 android.view.ViewRoot$CalledFromWr ...
- 【Java 基础篇】【第九课】继承
继承就是为了提高代码的复用率. 利用继承,我们可以避免代码的重复.让Woman类继承自Human类,Woman类就自动拥有了Human类中所有public成员的功能.我们用extends关键字表示继承 ...
- 使用C#发送正文带图片邮件
最近有个地方用到正文带图片的邮件发送功能,由于发送邮件调用的是web service,要求正文必须是string,而接收方要能看到图片,还不能单纯的添加一个图片地址链接,查阅了很多资料,基本上都是从头 ...
- Linux some command(continue...)
挂载硬盘 sudo mount -t ext4 /dev/sdb1 /media/hadoop 自动挂载相关 sudo blkid sudo fdisk -l vim /etc/fstab cat / ...