Backpack |

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?

Example

If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we can fill this backpack is 10. If the backpack size is 12. we can select[2, 3, 7] so that we can fulfill the backpack.

You function should return the max size we can fill in the given backpack.

分析:

看似这题是NP-hard问题,但是实际上可以用DP解决。result[i][j] 表示选取数组A中前i个数并且backpack size 是 j的时候,backpack剩余的size最小。

result[i][j] = Math.min(result[i - 1][j], result[i - 1][j - A[i]]);

 public class Solution {

     public int backPack(int m, int[] A) {
if (A == null || A.length == || m <= ) return m; int[][] result = new int[A.length][m + ];
for (int i = ; i < result.length; i++) {
for (int j = ; j <= m; j++) {
if (i == ) {
if (A[i] > j) {
result[i][j] = j;
} else {
result[i][j] = j - A[i];
}
} else {
if (A[i] > j) {
result[i][j] = result[i - ][j];
} else {
result[i][j] = Math.min(result[i - ][j], result[i - ][j - A[i]]);
}
} }
}
return m - result[A.length - ][m];
}
}

Backpack II

Given n items with size Ai and value Vi, and a backpack with size m. What's the maximum value can you put into the backpack?

Example

Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.

分析:

原理同上,转移方程如下:

maxValue[i][j] = Math.max(maxValue[i - 1][j], maxValue[i - 1][j - A[i]] + V[i]);

 public class Solution {
public int backPackII(int m, int[] A, int V[]) {
if (m <= || A == null || A.length == || V == null || V.length == ) return ; int[][] maxValue = new int[A.length][m + ]; for (int i = ; i < maxValue.length; i++) {
for (int j = ; j < maxValue[].length; j++) {
if ( i == ) {
if (A[i] <= j) {
maxValue[i][j] = V[i];
}
} else {
if (A[i] <= j) {
maxValue[i][j] = Math.max(maxValue[i - ][j], maxValue[i - ][j - A[i]] + V[i]);
} else {
maxValue[i][j] = maxValue[i - ][j];
}
}
}
}
return maxValue[maxValue.length - ][maxValue[].length - ];
}
}

参考请注明出处:cnblogs.com/beiyeqingteng/

Backpack | & ||的更多相关文章

  1. [LintCode] Backpack VI 背包之六

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  2. LintCode "Backpack"

    A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...

  3. LeetCode Backpack

    Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this ...

  4. Backpack III

    Description Given n kinds of items, and each kind of item has an infinite number available. The i-th ...

  5. Backpack IV

    Description Given an integer array nums[] which contains n unique positive numbers, num[i] indicate ...

  6. Backpack V

    Description Given n items with size nums[i] which an integer array and all positive numbers. An inte ...

  7. Backpack II

    Description There are n items and a backpack with size m. Given array A representing the size of eac ...

  8. Backpack VI

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  9. 0-1背包问题蛮力法求解(java版本)

    sloves: package BackPack; public class Solves {  public int[] DecimaltoBinary(int n,int m)  {   int ...

随机推荐

  1. Oracle 索引

    索引是建立在数据库表中的某些列的上面,是与表关联的,可提供快速访问数据方式,但会影响增删改的效率:常用类型(按逻辑分类):单列索引和组合索引.唯一索引和非唯一索引. 什么时候要创建索引 (1)在经常需 ...

  2. JQuery中的小技巧,,,连载中。。。

    获取下拉框中选中项的文本等操作 jQuery获取Select元素,并选择的Text和Value:  1.获取select 选中的 text : $("#ddlRegType").f ...

  3. Jquery-处理iframe的高度自适应

    超级简单的方法,也不用写什么判断浏览器高度.宽度啥的.下面的两种方法自选其一就行了.一个是放在和iframe同页面的,一个是放在test.html页面的.注意别放错地方了哦. iframe代码,注意要 ...

  4. c语言的数学函数ceil、floor、round

    头文件<math.h> 函数原型和作用 double ceil(double x); 向上取整 double floor(double x); 向下取整 double round(doub ...

  5. 积木(DP)问题

    问题:Do you remember our children time? When we are children, we are interesting in almost everything ...

  6. Spring MVC实现防止表单重复提交(转)

    Spring MVC拦截器+注解方式实现防止表单重复提交  

  7. HttpClient教程

    2.1.持久连接 两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间.Http连接需要的三次握手开销很大,这一开销对于比较小的http消息来说更大.但是如果我们直接使用已 ...

  8. jquery------.cycle的使用

    代码下载地址:http://malsup.github.io/jquery.cycle.all.js 把里面的代码复制到jquery.cycle.all.js里面 index.jsp <scri ...

  9. std::function,std::bind

    std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...

  10. Protocol Buffer技术详解(数据编码)

    Protocol Buffer技术详解(数据编码) 之前已经发了三篇有关Protocol Buffer的技术博客,其中第一篇介绍了Protocol Buffer的语言规范,而后两篇则分别基于C++和J ...