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. htpasswd 详解

    Apache htpasswd命令用法详解 htpasswd建立和更新存储用户名.密码的文本文件, 用于对HTTP用户的basic认证. # /usr/local/apache/bin/htpassw ...

  2. asp.net 捕获全局未处理异常的几种方法

    通过HttpModule来捕获未处理的异常[推荐] 首先需要定义一个HttpModule,并监听未处理异常,代码如下: public void Init(HttpApplication context ...

  3. @RestController注解下返回到jsp视图页面(转)(转)

    这个问题我也遇到过,下面的方法可以试试 蓝萝卜blu @RestController注解下返回到jsp视图页面 spring4.1中添加了@RestController注解很方便,集成了@Respon ...

  4. 【poj1144】 Network

    http://poj.org/problem?id=1144 (题目链接) 题意 求无向图的割点. Solution Tarjan求割点裸题.并不知道这道题的输入是什么意思,也不知道有什么意义= =, ...

  5. hihocoder 1169 猜数字

    传送门 时间限制:10000ms 单点时限:5000ms 内存限制:256MB 描述 你正在和小冰玩一个猜数字的游戏.小冰首先生成一个长为N的整数序列A1, A2, …, AN.在每一轮游戏中,小冰会 ...

  6. PS图层混合模式实例详解

          PS中的很多概念都和Core Graphics中的概念相通,比如蒙版.路径.裁剪.混合模式等等.如果你对Core Graphics中的混合模式不太理解,阅读本篇文章能让你对Core Gra ...

  7. java对象存储管理

    java程序在内存中的存储分配情况: 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享, ...

  8. vagrant 错误记录

    使用Vagrant配置本地开发环境 从二零一四年开始使用vagrant+VirtualBox搭建linux开发环境,配置简单灵活,后台运行占用内存少,比vmware好用很多,果断弃用vmware转投v ...

  9. 极大似然估计、贝叶斯估计、EM算法

    参考文献:http://blog.csdn.net/zouxy09/article/details/8537620 极大似然估计 已知样本满足某种概率分布,但是其中具体的参数不清楚,极大似然估计估计就 ...

  10. Java web小记

    1.Java Web设置页面刷新的方法(两种): response.setHeader("refresh", "0.3," + request.getHeade ...