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 ...
随机推荐
- python 使用requests 请求 https 接口 ,取消警告waring
response = requests.request("POST", url, timeout=20, data=payload, headers=headers, proxie ...
- 项目十八-Hadoop+Hbase分布式集群架构“完全篇”
本文收录在Linux运维企业架构实战系列 前言:本篇博客是博主踩过无数坑,反复查阅资料,一步步搭建,操作完成后整理的个人心得,分享给大家~~~ 1.认识Hadoop和Hbase 1.1 hadoop简 ...
- linux运维、架构之路-MySQL多实例
一.MySQL多实例介绍 一台服务器上开启多个不同的服务端口(3306,3307,3308),运行多个MySQL服务进程,共用一套MySQL安装程序,多实例MySQL在逻辑上看是 ...
- Nginx读书笔记
... upstream proxy_svrs { server http://192.168.1.1:8001/uri/; server http://192.168.1.2:8001/uri/; ...
- 二 python并发编程之多进程-重点
一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...
- 分享 php array_column 函数 无法在低版本支持的 修改
function i_array_column($input, $columnKey, $indexKey=null){ if(!function_exists('array_column')){ $ ...
- Python入门必学:递归函数正确的操作使用方法,案例详解
递归函数,在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact(n)表示,可以 ...
- 海量数据处理算法—BitMap
1. Bit Map算法简介 来自于<编程珠玑>.所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空 ...
- 批量导出ppt中内嵌的图片
某个ppt中很多页,然后插入了很多图片,且图片都是被压缩的,看起来非常费劲,所以想着一次性把图片另存为,找了接近一个小时,终于被我找到啦,分享给大家: 1.直接把ppt的后缀修改为rar 2.解压ra ...
- laravel5.2总结--csrf保护
CSRF攻击: CSRF 顾名思义,是伪造请求,冒充用户在站内的正常操作.我们知道,绝大多数网站是通过 cookie 等方式辨识用户身份(包括使用服务器端 Session 的网站,因为 S ...