Backpack1-01
Given_n_items with size Ai, an integer_m_denotes the size of a backpack. How full you can fill this backpack?
Example
If we have4
items with size[2, 3, 5, 7]
, the backpack size is 11, we can select[2, 3, 5]
, so that the max size we can fill this backpack is10
. If the backpack size is12
. we can select[2, 3, 7]
so that we can fulfill the backpack.
You function should return the max size we can fill in the given backpack.
Note
我们把每个物品的大小当作每个物品的价值
状态转移是放还是不放,注意要判断内层循环的value值是不是比外层循环对于的物品值大
否则直接是之前状态的值延续dp[i + 1][j] = dp[i][j]
外层循环0到i不包括,i状态写i+1状态,内层循环0到最大限制(包括)
Code
Last updated