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的更多相关文章

  1. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  2. 对背包问题(Knapsack Problem)的算法探究

    对背包问题(Knapsack Problem)的算法探究 至繁归于至简,这次自己仍然用尽可能易理解和阅读的解决方式. 1.问题说明: 假设有一个背包的负重最多可达8公斤,而希望在背包中装入负重范围内可 ...

  3. 动态规划法(四)0-1背包问题(0-1 Knapsack Problem)

      继续讲故事~~   转眼我们的主人公丁丁就要离开自己的家乡,去大城市见世面了.这天晚上,妈妈正在耐心地帮丁丁收拾行李.家里有个最大能承受20kg的袋子,可是妈妈却有很多东西想装袋子里,已知行李的编 ...

  4. FZU 2214 ——Knapsack problem——————【01背包的超大背包】

    2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

  5. FZU-2214 Knapsack problem(DP使用)

    Problem 2214 Knapsack problem Accept: 863    Submit: 3347Time Limit: 3000 mSec    Memory Limit : 327 ...

  6. knapsack problem 背包问题 贪婪算法GA

    knapsack problem 背包问题贪婪算法GA 给点n个物品,第j个物品的重量,价值,背包的容量为.应选哪些物品放入包内使物品总价值最大? 规划模型 max s.t. 贪婪算法(GA) 1.按 ...

  7. [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 ...

  8. 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 ...

  9. (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 ...

随机推荐

  1. Rhadoop安装

    1.ubuntu,hadoop,R,jdk安装好 2.下载Rhadoop项目的的三个包,rmr,hdfs,rHBase存放到Downloads/R. 3.切换到root 4.安装依赖的库 ~R CMD ...

  2. BZOJ3679: 数字之积(数位dp)

    题意 题目链接 Sol 推什么结论啊. 直接大力dp,$f[i][j]$表示第$i$位,乘积为$j$,第二维直接开map 能赢! /* */ #include<iostream> #inc ...

  3. innobackup 参数

    innobackupex [--compress] [--compress-threads=NUMBER-OF-THREADS] [--compress-chunk-size=CHUNK-SIZE] ...

  4. python 时间加8小时后的时间

    eta_temp = one['arrival'].encode('utf-8') fd = datetime.datetime.strptime(eta_temp, "%Y-%m-%dT% ...

  5. node操作mogondb数据库的封装

    注:摘自网络 上面的注释都挺详细的,我使用到了nodejs的插件mongoose,用mongoose操作mongodb其实蛮方便的. 关于mongoose的安装就是 npm install -g mo ...

  6. 在windows server 2008 64位服务器上配置php环境

    1.安装windows2008 R2 46位 安装2008 R2 关键步骤,网上有很多诸如此类的安装介绍.在些南昌网站建设公司百恒网络工程师就不作详细介绍.关键是要选择适合实际应用的部署.    2. ...

  7. 高并发架构系列:如何从0到1设计一个类Dubbo的RPC框架

    在过去持续分享的几十期阿里Java面试题中,几乎每次都会问到Dubbo相关问题,比如:“如何从0到1设计一个Dubbo的RPC框架”,这个问题主要考察以下几个方面: 你对RPC框架的底层原理掌握程度. ...

  8. 三分钟明白 Activity工作流 -- java运用

    一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假申请——上级口头同意——上级将请假记录下来——月底将请假记录上交公司——公司将请假录入电脑 采用工作 ...

  9. 通过uboot传参设置mtd分区流程源码分析

    因为公司同事反映他使用的开板无法将根目录下的ip_work目mounth成功,由于本人当时没有去现场查看问题,只是象征性的询问内核是否创建了/dev/mtdblock5设备节点,因为该开发板默认是挂载 ...

  10. POJ1426-Find The Multiple(搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42035   Accepted: 176 ...