public class Solution {
/**
* @param m: An integer m denotes the size of a backpack
* @param A & V: Given n items with size A[i] and value V[i]
* @return: The maximum value
*/
public int backPackII(int m, int[] A, int V[]) {
// write your code here
int bb[][]=new int[A.length][m+1],i=0,j=0;
//bb[][0]
for (i = 0; i < A.length ; ++i)
{
bb[i][0] = 0;
}
//bb[][]
for (j = 1; j <m + 1; ++j){
if (A[0]>j) bb[0][j] = 0;
else bb[0][j] = V[0];
for (i = 1; i <A.length ; ++i){
if (A[i]>j) bb[ i][j] = bb[i - 1][j];
else bb[i][j] = Math.max(bb[i - 1][j], bb[i - 1][j - A[i]] + V[i]);
}
}
return bb[A.length-1][m];
}
}
LintCode(M)背包问题2
最新推荐文章于 2021-05-16 13:32:13 发布