L125 Backpack II
Givenn_items with size Aiand 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 size10
. The maximum value is9
.
O(n x m) memory is acceptable, can you do it in O(m) memory?
每个只能取一次。所以取上一行dp[i - 1]的
例子:
size | val | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ||
2 | 1 | 1 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
3 | 5 | 2 | 0 | 0 | 1 | 5 | 5 | 6 | 6 | 6 | 6 | 6 | 6 |
5 | 2 | 3 | 0 | 0 | 1 | 5 | 5 | 6 | 6 | 6 | 7 | 7 | 8 |
7 | 4 | 4 | 0 | 0 | 1 | 5 | 5 | 6 | 6 | 6 | 7 | 7 | 9 |
Last updated