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 ...
随机推荐
- Oracle创建表格报ORA-00906:缺失左括号错误解决办法
来源于:http://www.linuxidc.com/Linux/2013-06/85297.htm 解决办法: create table myTable(id number(5,2),name v ...
- DML语言练习,数据增删改查,复制清空表
Connected Connected as TEST@ORCL SQL> select * from t_hq_bm; BUMBM BUMMC DIANH ---------- ------- ...
- MVC升级以后出现"当前上下文中不存在ViewBag"的问题解决
把自己的项目从MVC4升级到了MVC5,结果问题一大堆,View的设计环境出现了"当前上下文中不存在ViewBag"的问题: 虽然不影响编译,但是看了总是不爽,而且语法提示也没有了 ...
- SPOJ QTREE 树链剖分
树链剖分的第一题,易懂,注意这里是边. #include<queue> #include<stack> #include<cmath> #include<cs ...
- SQL Server 2008 R2导出数据脚本的方法
以前看到有些朋友说必须SQL Server 2008才能导出包含数据的脚本,后来仔细研究发现其实SQL Server 2008 R2也是可以的,只需在导出的时候在高级中设置一下即可. 1.首先在数据库 ...
- VB中的属性、方法和事件概念解析
Visual Basic 语言中的所有对象都有它们自己的属性.方法和事件,其中包括窗体和控件.可以将属性视为对象的特性,将方法视为对象的操作,而将事件视为对象的响应. 日常生活中的对象(如氦气球)也具 ...
- 39.Android版本小知识
中文名----英文名----版本----对应API Level 棉花糖 Marshmallow - 6.0.1_r10 - API 23棉花糖 Marshmallow - 6.0.0_r5 - API ...
- on the way to Peking University
明天就要去北京参加北大夏令营了,希望这次能有所斩获! on the way to Peking University
- Core Animation编程指南
本文是<Core Animation Programming Guide>2013-01-28更新版本的译文.本文略去了原文中关于OS X平台上Core Animation相关内容.因为原 ...
- Emgu学习之(二)——图像读取、显示、保存
visual Studio Community 2015 工程和源代码:http://pan.baidu.com/s/1o6u5Fdw 内容 在这篇文章中将提到以下内容: 从文件中读取图像 Image ...