0/1 knapsack problem
Problem statement
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?
Solution
0/1 knapsack problem is a classical dynamic programming model. There is a knapsack with the capacity of m, you should find the maximum volume can be filled in.
Still, we need:
- DP memory and the representation
- The initialization of DP memory
- DP formula
- Return value.
DP memory and the representation
Suppose, size is the number of elements in A.
A two dimension array: dp[size + 1][m + 1]
- dp[i][j]: means the maximum volume formed by first i elements whose volume is at most j.
The key word is the first and at most.
- The first means there are i + 1 elements.
- At most means the total volume can not exceed j.
Initialization
For a two dimension DP memory, normally, we should initialize the first row and column, and start from i = 1 and j = 1. The initialization comes from general knowledge.
- dp[0][i]: first 0 elements can form at most i volume. Obviously, the initialization is 0 since we can get nothing if there is no elements.
- dp[i][0]: first i elements can form at most 0 volume. Obviously, the initialization is 0 since we can get 0 volume by any elements.
DP formula
For current element A[i], we need to know what is the maximum volume can get if we add it into the backpack.
- dp[i][j] = dp[i - 1][j] if A[i - 1] is greater than j
- dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - A[i - 1]]) if j >= A[i - 1], we find the maximum value.
Return value.
Just return dp[size][m]
Time complexity is O(size * m)
class Solution {
public:
/**
* @param m: An integer m denotes the size of a backpack
* @param A & V: Given n items with size A[i] and value V[i]
* @return: The maximum value
*/
int backPackII(int m, vector<int> A, vector<int> V) {
// write your code here
// write your code here
int size = A.size();
//vector<vector<int>> dp(size + 1, vector<int>(m + 1, 0));
int dp[size + ][m + ] = {};
for(int i = ; i <= size; i++){
for(int j = ; j <= m; j++){
dp[i][j] = dp[i - ][j];
if(j >= A[i - ]){
dp[i][j] = max(dp[i][j], V[i - ] + dp[i - ][j - A[i - ]]);
}
}
}
return dp[size][m];
}
};
0/1 knapsack problem的更多相关文章
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- 对背包问题(Knapsack Problem)的算法探究
对背包问题(Knapsack Problem)的算法探究 至繁归于至简,这次自己仍然用尽可能易理解和阅读的解决方式. 1.问题说明: 假设有一个背包的负重最多可达8公斤,而希望在背包中装入负重范围内可 ...
- 动态规划法(四)0-1背包问题(0-1 Knapsack Problem)
继续讲故事~~ 转眼我们的主人公丁丁就要离开自己的家乡,去大城市见世面了.这天晚上,妈妈正在耐心地帮丁丁收拾行李.家里有个最大能承受20kg的袋子,可是妈妈却有很多东西想装袋子里,已知行李的编 ...
- FZU 2214 ——Knapsack problem——————【01背包的超大背包】
2214 Knapsack problem Accept: 6 Submit: 9Time Limit: 3000 mSec Memory Limit : 32768 KB Proble ...
- FZU-2214 Knapsack problem(DP使用)
Problem 2214 Knapsack problem Accept: 863 Submit: 3347Time Limit: 3000 mSec Memory Limit : 327 ...
- knapsack problem 背包问题 贪婪算法GA
knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...
- [DP] The 0-1 knapsack problem
Give a dynamic-programming solution to the 0-1 knapsack problem that runs in O(nW) time, where n is ...
- FZU - 2214 Knapsack problem 01背包逆思维
Knapsack problem Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...
- (01背包 当容量特别大的时候) Knapsack problem (fzu 2214)
http://acm.fzu.edu.cn/problem.php?pid=2214 Problem Description Given a set of n items, each with a ...
随机推荐
- 移动端调试利器-vConsole
现在移动端开发越来越火,随之而来的问题也越来越多,今天给大家介绍一款移动端调试神器,vconsole. 一.先引用文件,可以从https://www.bootcdn.cn/vConsole/下载,也可 ...
- 操作系统(1)_操作系统结构_李善平ppt
cpu和内存之间通过地址总线.数据总线.控制总线连接.外部总线连接外部设备.下图有问题,内存和外设没有直接连接.同一组总线,CPU和内存连接的时候硬盘就不能和内存连接,否则有冲突,core和core之 ...
- PAT 乙级 1024
题目 题目地址:PAT 乙级 1024 题解 模拟题,重点需要考虑到各种不同情况:简单来说一下: 因为输入格式固定,所以把不同的部分分别存储和处理可以在很大程度上简化运算:其中需要考虑最多的就是小数部 ...
- Mbps、Kbps、bps、kb、mb区别和换算
Mbps 即 Milionbit pro second(百万位每秒) Kbps 即 Kilobit pro second(千位每秒) bps 即 bit pro second(位每秒) 速度单位,bi ...
- javascript 计算倒计时
function timeDown(second) { var month = '', day = '', hour = '', minute = ''; if (second >= 86400 ...
- BootCDN 开源项目免费 CDN 加速服务,Jquery插件库
2017-11-17 19:38:32 免费好用的在线 css js 文件引用 BootCDN - Bootstrap 中文网开源项目免费 CDN 加速服务 Jquery插件库 .
- python3.7 time模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 time模块 #time模块没有time.py文件,是内置到解释 ...
- HDU 6053 ( TrickGCD ) 分块+容斥
TrickGCD Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- DFS:Prime Ring Problem(素数环)
解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...
- loj2074 「JSOI2016」灯塔
loj 题面错的--去bzoj上看吧qwq 观察到 \(\sqrt{|i-j|}\) 的取值只有 \(\sqrt{n}\) 级别个,然后就很显然了,rmq. #include <iostream ...