Givenn_items with size Aiand value Vi, and a backpack with size_m. What's the maximum value can you put into the backpack?

Notice

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

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 size10. The maximum value is9.

//transition to for each possible weight m what's the maximum value
    //last step: in the best solution:
                 //a. the first A[i - 1] item already fullfill
                 //b. the first A[i - 1] item fufills j - A[i - 1]
    //f[i][j] represents the maximum value when use the first i to fullfill size j
    //f[i][j] = Max{f[i - 1][j], f[i - 1][j - A[i - 1]] + V[i - 1]}
    //init
    //f[i][0] 0<=i<=n = 0
    //f[0][j] 1<=j<m = 0
    public int backPackII(int m, int[] A, int V[]) {
        // write your code here
        if (m == 0) {
            return 1;
        }

        int n = A.length;
        int[][] f = new int[n + 1][m + 1];

        //init
        int i, j;
        for (i = 0; i <= n; i++) {
            f[i][0] = 0;
        }

        for (j = 1; j <= n; j++) {
            f[0][j] = 0;
        }

        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                f[i][j] = f[i - 1][j];
                if (j - A[i - 1] >= 0) {
                    f[i][j] = Math.max(f[i][j], f[i - 1][j - A[i - 1]] + V[i - 1]);
                }
            }
        }

        int ans = 0;
        for (j = 0; j <= m; j++) {
            ans = Math.max(ans, f[n][j]);
        }
        return ans;
    }

O(M) space, M is the size of the back pack

public int backPackII(int m, int[] A, int V[]) {
        // write your code here
        if (m == 0) {
            return 1;
        }

        int n = A.length;
        int[][] f = new int[2][m + 1];

        //init
        int i, j;
        int now = 0;
        int old = 0;
        f[0][0] = 0;
        f[1][0] = 0;

        for (j = 1; j <= n; j++) {
            f[0][j] = 0;
        }

        for (i = 1; i <= n; i++) {
            old = now;
            now = 1 - now;
            for (j = 1; j <= m; j++) {
                f[now][j] = f[old][j];
                if (j - A[i - 1] >= 0) {
                    f[now][j] = Math.max(f[now][j], f[old][j - A[i - 1]] + V[i - 1]);
                }
            }
        }

        int ans = 0;
        for (j = 0; j <= m; j++) {
            ans = Math.max(ans, f[now][j]);
        }
        return ans;
    }

O(M) space M is the size of the backpack with one dimensional array

public int backPackII(int m, int[] A, int V[]) {
        // write your code here
        if (m == 0) {
            return 1;
        }

        int n = A.length;
        int[] f = new int[m + 1];

        //init
        int i, j;

        for (j = 0; j <= n; j++) {
            f[j] = 0;
        }

        for (i = 1; i <= n; i++) {
            for (j = m; j >= 1; j--) {
                if (j - A[i - 1] >= 0) {
                    f[j] = Math.max(f[j], f[j - A[i - 1]] + V[i - 1]);
                }
            }
        }

        int ans = 0;
        for (j = 0; j <= m; j++) {
            ans = Math.max(ans, f[j]);
        }
        return ans;
    }

results matching ""

    No results matching ""