Lintcode DP Backpack

###Task1 Example If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we can fill this backpack is 10. If the backpack size is 12. we can select [2, 3, 7] so that we can fulfill the backpack.

You function should return the max size we can fill in the given backpack.

Note You can not divide any item into small pieces.

Challenge O(n x m) time and O(m) memory.

O(n x m) memory is also acceptable if you do not know how to optimize memory.

###Java

public class Solution {
    /**
     * @param m: An integer m denotes the size of a backpack
     * @param A: Given n items with size A[i]
     * @return: The maximum size
     */
    public int backPack(int m, int[] A) {
        // write your code here
        if (A == null || A.length == 0) {
            return 0;
        }
        int n = A.length;
        boolean[][] dp = new boolean[n + 1][m + 1];
        dp[0][0] = true;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= m; j++) {
                dp[i + 1][j] = dp[i][j];
                if (j >= A[i] && dp[i][j - A[i]]) {
                    dp[i + 1][j] = true;
                }
            }
        }
        for (int i = m; i >= 0; i--) {
            if (dp[n][i]) {
                return i;
            }
        }
        return 0;
    }
}

###point boolean d[i][j]: For the first i items, can we fill a backpack of size j? true or false.

d[i][j] = d[i-1][j] || (j>=A[i-1] && d[i-1][j-A[i-1]]).

d[0][0] = true;

We can use 1D array to perform the DP.

d[j] = d[j] || d[j-A[i-1]].

NOTE: for 1D array, the j must be decreased from m to 0 rather increasing from 0 to m!

###Task2

Given n items with size Ai and value Vi, and a backpack with size m. What’s the maximum value can you put into the backpack?

Have you met this question in a real interview? Yes Example Given 4 items with size [2, 3, 5, 7] and value [1, 5, 2, 4], and a backpack with size 10. The maximum value is 9.

Note You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m.

Challenge O(n x m) memory is acceptable, can you do it in O(m) memory?

###Java

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
        if (A.length != V.length) {
            return 0;
        }
        int n = A.length;
        int[][] dp = new int[n + 1][m + 1];
        dp[0][0] = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j <= m; j++) {
                if (j < A[i - 1]) {
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - A[i - 1]] + V[i  -1]);
                }
            }
        }
        return dp[n][m];
    }
}

###Points