Backpack | & ||
Backpack |
Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?
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?
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 | & ||的更多相关文章
- [LintCode] Backpack VI 背包之六
Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...
- LintCode "Backpack"
A simple variation to 0-1 Knapsack. class Solution { public: /** * @param m: An integer m denotes th ...
- LeetCode Backpack
Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this ...
- Backpack III
Description Given n kinds of items, and each kind of item has an infinite number available. The i-th ...
- Backpack IV
Description Given an integer array nums[] which contains n unique positive numbers, num[i] indicate ...
- Backpack V
Description Given n items with size nums[i] which an integer array and all positive numbers. An inte ...
- Backpack II
Description There are n items and a backpack with size m. Given array A representing the size of eac ...
- Backpack VI
Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...
- 0-1背包问题蛮力法求解(java版本)
sloves: package BackPack; public class Solves { public int[] DecimaltoBinary(int n,int m) { int ...
随机推荐
- zabbix_agent安装(Centos+Ubuntu)
Centos安装 安装依赖包 yum -y install mysql-devel libcurl-devel net-snmp-devel 添加用户 groupadd zabbix use ...
- maven_创建quickstart模板时异常
错误信息: Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart:1.1 from an ...
- hdu5007 字符串
字符串问题.是否出现iPhone Apple等词:我考虑时想到既然是否有这些词,可以写map标记一下:然后又最长的是iPhone,6个单词,所以第一个for遍历所有单词 然后在一个for(1~6),用 ...
- Jquery-获取同级标签prev,prevAll,next,nextAll
1.next([expr]): 获取指定元素的下一个同级元素(注意是下一个同级元素哦) 参数可有可无,参数设定遵循jquery选择器规则 <!DOCTYPE html> <html& ...
- BZOJ2818 欧拉函数
题意:求1--n中满足gcd(x,y)的值为质数的数对(x,y)的数目 ( (x,y)和(y,x)算两个 ) sol: 设p[i]是一个质数,那么以下两个命题是等价的: 1.gcd(x,y)=1 2. ...
- php编程常用经验
1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的"函数&quo ...
- 统计网站访问量,以GD2库图像形式输出
index.php页面<?php session_start(); if($_SESSION[temp]==""){ //判断$_SESSION[temp]=="& ...
- 两款CSS3样式可视化在线生成工具
CSS3随着浏览器的升级已经被越来越广泛的运用,合理的运用CSS3可以使你的网站更加美观,并且之前只能用js才能实现的效果也已经可以直接用 CSS3来实现.但是虽然如此,很多浏览器对CSS3的支持还都 ...
- centos安装gitlab
原文链接: http://www.centoscn.com/image-text/install/2015/0320/4929.html http://www.01happy.com/centos-6 ...
- Android手机 Fildder真机抓包
Fiddler是一个http调试代理,它能 够记录所有的你电脑和互联网之间的http通讯,Fiddler 可以也可以让你检查所有的http通讯,设置断点,以及Fiddle 所有的“进出”的数据(指co ...