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的更多相关文章

  1. Lintcode: Minimum Adjustment Cost

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

  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. Minimum Transport Cost Floyd 输出最短路

    These are N cities in Spring country. Between each pair of cities there may be one transportation tr ...

随机推荐

  1. Spring的Bean之Bean的基本概念

    从前面我们知道spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...

  2. centos 7.0 nginx 1.7.9 安装过程

    系统用的是centos 7.0最小化安装 我现在安装完了 写一下步骤 还没完全搞懂 首先安装GCC [root@localhost ~]# yum install -y gcc gcc-c++ 已加载 ...

  3. Css常用收集

    /*-------------------------------------- 圆角*/ -webkit-border-radius: 4px;  -moz-border-radius: 4px; ...

  4. OC-类方法

    类方法 1. 基本概念 直接可以用类名来执行的方法(类本身会在内存中占据存储空间,里面有类\对象方法列表) 2. 类方法和对象方法对比 1)  对象方法 以减号-开头 只能让对象调用,没有对象,这个方 ...

  5. Working with Transactions (EF6 Onwards)

    Data Developer Center > Learn > Entity Framework > Get Started > Working with Transactio ...

  6. RabbitMQ 用户角色详解

    RabbitMQ的用户角色分类:none.management.policymaker.monitoring.administrator RabbitMQ各类角色描述:none不能访问 managem ...

  7. Log4Net使用方法

    项目里都会用到日志记录..特别是在本地跑的欢畅..一上服务器就嗝屁的时候..日志会帮你大忙.. Log4net算是一种应用的最广泛的日志记录方式了..下面来简略的说下他的用法.. 先下载log4net ...

  8. 柱状堆积图Echarts

    Map<String,Object> map = new HashMap<String, Object>(); //图例的千人.双百 HashMap<String, St ...

  9. jQuery1.9.1源码分析--数据缓存Data模块

    jQuery1.9.1源码分析--数据缓存Data模块 阅读目录 jQuery API中Data的基本使用方法介绍 jQuery.acceptData(elem)源码分析 jQuery.data(el ...

  10. 将本地的新建的web Form页面放到服务器提示错误;

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs&q ...