lintcode: 最小调整代价
题目
最小调整代价
给一个整数数组,调整每个数的大小,使得相邻的两个数的差小于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少。
对于数组[1, 4, 2, 3]和target=,最小的调整方案是调整为[2, 3, 2, 3],调整代价之和是2。返回2。
你可以假设数组中每个整数都是正整数,且小于等于。
解题
参考博客 比较复杂
方法一
用1 到100内的数,替换数组中的每位数字
public class Solution {
/**
* @param A: An integer array.
* @param target: An integer.
*/
public static int MinAdjustmentCost(ArrayList<Integer> A, int target) {
// write your code here
if (A == null) {
return 0;
}
return rec(A, new ArrayList<Integer>(A), target, 0);
}
/*
* SOL 1:
* 最普通的递归方法。
* */
public static int rec(ArrayList<Integer> A, ArrayList<Integer> B, int target, int index) {
int len = A.size();
if (index >= len) {
// The index is out of range.
return 0;
}
int dif = 0;
int min = Integer.MAX_VALUE;
// If this is the first element, it can be from 1 to 100;
for (int i = 0; i <= 100; i++) {
if (index != 0 && Math.abs(i - B.get(index - 1)) > target) {
continue;
}
B.set(index, i);// index位置的值更新为 i
dif = Math.abs(i - A.get(index)); // 记录差值
dif += rec(A, B, target, index + 1); // 中间迭代
min = Math.min(min, dif); // 计算最小值
// 回溯
B.set(index, A.get(index));
}
return min;
}
}
超时
方法二
定义一个数组M存储中间最优过程
M[i][j] 表示第i个位置的数换成j的最优值,j取值1 - 100
public class Solution {
/**
* @param A: An integer array.
* @param target: An integer.
*/
public static int MinAdjustmentCost(ArrayList<Integer> A, int target) {
// write your code here
if (A == null) {
return 0;
}
int n = A.size();
int[][] M = new int[n][101];
for(int i=0;i<n;i++){
for(int j=0;j<101;j++){
M[i][j] = Integer.MAX_VALUE;
}
}
return rec(A, new ArrayList<Integer>(A),M, target, 0);
}
public static int rec(ArrayList<Integer> A, ArrayList<Integer> B, int[][] M,int target, int index) {
int len = A.size();
if (index >= len) {
// The index is out of range.
return 0;
}
int dif = 0;
int min = Integer.MAX_VALUE;
// If this is the first element, it can be from 1 to 100;
for (int i = 0; i <= 100; i++) {
if (index != 0 && Math.abs(i - B.get(index - 1)) > target) {
continue;
}
if(M[index][i]!=Integer.MAX_VALUE){
dif = M[index][i];
min = Math.min(min,dif);
continue;
}
B.set(index, i);// index位置的值更新为 i
dif = Math.abs(i - A.get(index)); // 记录差值
dif += rec(A, B, M,target, index + 1); // 中间迭代
M[index][i] = dif; // 存储中间结果
min = Math.min(min, dif); // 计算最小值
// 回溯
B.set(index, A.get(index));
}
return min;
}
}
上面博客中还要两种方法,留着下次
lintcode: 最小调整代价的更多相关文章
- lintcode-91-最小调整代价
91-最小调整代价 给一个整数数组,调整每个数的大小,使得相邻的两个数的差不大于一个给定的整数target,调整每个数的代价为调整前后的差的绝对值,求调整代价之和最小是多少. 注意事项 你可以假设数组 ...
- [vijos P1524] 最小监视代价
历时四天(本周三至本周六),本人的第一道网络流题目终于通过了…虽然这么慢才搞懂很大程度是因为脑子笨,但是还是要吐槽一下: (1)选的这道题吧居然是无向图,对于初学者我表示呵呵,昨晚到现在一直在纠结怎么 ...
- 【网络流24题】 No.12 软件补丁问题(最小转移代价 最短路)
[题意] T 公司发现其研制的一个软件中有 n 个错误, 随即为该软件发放了一批共 m 个补丁程序. 每一个补丁程序都有其特定的适用环境, 某个补丁只有在软件中包含某些错误而同时又不包含另一些错误时才 ...
- [CTCI] 最小调整有序
最小调整有序 题目描述 有一个整数数组,请编写一个函数,找出索引m和n,只要将m和n之间的元素排好序,整个数组就是有序的.注意:n-m应该越小越好,也就是说,找出符合条件的最短序列. 给定一个int数 ...
- 最小总代价 状压DP
描述 n个人在做传递物品的游戏,编号为1-n. 游戏规则是这样的:开始时物品可以在任意一人手上,他可把物品传递给其他人中的任意一位:下一个人可以传递给未接过物品的任意一人. 即物品只能经过同一个人一次 ...
- Vijos 1456 最小总代价 (状压dp)
看到这道题n只有16,就可以想到状压dp 每个人只有经过或者没经过,那就用1表示经过,0表示没经过 但是不是当前在谁那里,所以再加一维来记录 所以f[state][i]表示在物品在i,当前的状态是st ...
- BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡:贪心【最小匹配代价】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3399 题意: 给你一个数列a,和一个可变换顺序的序列b(数列长度≤25000). a增加一 ...
- vijos 1524 最小监视代价
背景 看到Vijos上此类型的题目较少,特地放一道上来给大家练练. 描述 由于yxy小朋友做了一些不该做的事,他被jzp关进了一个迷宫里.由于jzp最近比较忙,疏忽大意了一些,yxy可以在迷宫中任意走 ...
- [程序员代码面试指南]递归和动态规划-最小编辑代价(DP)
问题描述 输入 原字符串StrOrg,目标字符串StrTarget,插入.删除.替换的编辑代价ic,dc,rc.输出将原字符串编辑成目标字符串的最小代价. 解题思路 状态表示 dp[i][j]表示把s ...
随机推荐
- 使用 Swift 制作一个新闻通知中心插件(2)
我们在第一部分的文章中详细讲解了创建一个通知中心插件的整体过程.我们成功的在通知中心里面显示了新闻列表.但是截止到目前,我们还不能从通知中心的列表中查看新闻的详细内容.在这次的教程中,我们就以上次的教 ...
- 横屏下的ImagePickerController
Try this way.... As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You h ...
- Question about pairing/bonding?
Except that on android you can bypass the pairing dialog if you know the PIN in advance through a di ...
- [转]windows 软链接的建立及删除
[转]windows 软链接的建立及删除 http://blog.chinaunix.net/uid-74941-id-3764093.html 1.建立举例 ##建立d:develop链接目录,指向 ...
- Spring集成hibernate错误
八月 25, 2016 7:55:31 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRule ...
- 命令行连接wifi
ubuntu没有图形界面,插入无线网卡后启动不能连接无线. 看这个帖子 http://askubuntu.com/questions/138472/how-do-i-connect-to-a-wpa- ...
- C#委托详解(2):实现方式大全
本系列文章将详细探讨C#中的委托,列举其主要的实现方式,并分析其在设计层面和编码层面带来的好处,最后会讨论其安全性和执行效率等. 接上篇(C#委托详解(1):什么是委托)介绍完什么是委托之后,来看看C ...
- 【Simplify Path】cpp
题目: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&quo ...
- 老陈 WPF
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 802.11 wireless 1(主要还是学习ccna wireless的体系)
802.11 wireless 1(主要还是学习ccna wireless的体系)ISM频带(ISM band starts early 1990s)900MHZ 2.4GHZ 5GHZ 四种 ...