2017.9.7
新加入了体积和价值,这两个限制变量。
那么dp[j] 表示的是,对于体积j,可以得到的最大的价值。
dp[j] = max{dp[j],dp[j-体积[i] + 价值[i]}
public class Solution {
/*
* @param m: An integer m denotes the size of a backpack
* @param A: Given n items with size A[i]
* @param V: Given n items with value V[i]
* @return: The maximum value
*/
public int backPackII(int m, int[] A, int[] V) {
// 背包的大小为m,各个物品的体积为V,各个物品的价值为A。write your code here
int length = A.length;
int []dp = new int[m+1];
for(int i = 0; i < length; i++){
for(int j = m ;j >= 1;j--){
if(j >= A[i]){
dp[j] = Math.max(dp[j], dp[j - A[i]] + V[i]);
}
}
}
return dp[m];
}
}