LintCode(M)背包问题2

来源:互联网 发布:郁可唯 独家记忆 知乎 编辑:程序博客网 时间:2024/05/20 04:48
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];    }}
0 0