https://www.cnblogs.com/kkbill/p/12081172.html
-
如果选择它,那么,当前背包的最大价值等于" i 号物品的价值“ 加上 "减去 “i 号物品占用的空间后剩余的背包空间所能存放的最大价值”,即 d p [ i ] [ k ] = v a l u e [ i ] + d p [ i − 1 ] [ k − w e i g h t [ i ] ] dp[i][k] = value[i] + dp[i-1][k-weight[i]] dp[i][k]=value[i]+dp[i−1][k−weight[i]];
-
如果不选择它,那么,当前背包的价值就等于前 i-1 个物品存放在背包中的最大价值,即 d p [ i ] [ k ] = d p [ i − 1 ] [ k ] dp[i][k] = dp[i-1][k] dp[i][k]=dp[i−1][k]
-
So: dp[i][k] 的结果取两者的较大值,即:
d p [ i ] [ k ] = m a x ( v a l u e [ i ] + d p [ i − 1 ] [ k − w e i g h t [ i ] ] , d p [ i − 1 ] [ k ] ) dp[i][k] = max(value[i] + dp[i-1][k-weight[i]], dp[i-1][k]) dp[i][k]=max(value[i]+dp[i−1][k−weight[i]],dp[i−1][k])