LintCode(M)背包问题2

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 1; ++j){

        if (A[0]>j) bb[0][j] = 0;
        else bb[0][j] = V[0];

        for (i = 1; 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)