Backpack II
Backpack II (0 - 1背包问题)
0-1 knapsack problem
单次选择 + 最大价值
Question
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?
Notice
You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.
Example
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
.
Challenge
O(n x m) memory is acceptable, can you do it in O(m) memory?
Tags
LintCode Copyright Dynamic Programming Backpack
Related Problems
Medium Backpack VI 30 % Medium Backpack V 39 % Medium Backpack IV 36 % Hard Backpack III 50 % Medium Backpack 23 %
Analysis
https://github.com/tianyicui/pack/blob/master/V2.pdf
最基础的背包问题,特点是:每种物品仅有一件,可以选择放或不放。0 - 1
子问题定义状态:即 F[i, v]
表示前 i 件物品放入一个容量为 v
的背包可以获得 的最大价值
其状态转移方程便是: F[i, v] = max{F[i − 1, v], F[i − 1, v − Ci ] + Wi}
DP四要素
状态 State
f[i][j]
表示前 i 个物品当中选一些物品组成容量为 j 的最大价值
方程 Function
f[i][j] = max(f[i-1][j], f[i-1][j-A[i-1]] + V[i-1])
;
初始化 Initialization
f[0][0] = 0
;
答案 Answer
f[n][s]
时间复杂度 O(n*s)
Solution
2D DP
1D DP space Optimized
用一维数组优化
Last updated