Backpack

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?

 Notice

You can not divide any item into small pieces.

Have you met this question in a real interview? Yes
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.

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.

Tags
LintCode Copyright Backpack Dynamic Programming
public int backPack(int m, int[] A) {
        // write your code here
        boolean[][] f = new boolean[A.length + 1][m + 1];

        //initialize
        f[0][0] = true;
        for (int i = 1; i <= m; i++) {
            f[0][i] = false;
        }

        for (int i = 1; i <= A.length; i++) {
            f[i][0] = true;
        }

        //function
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= A.length; j++) {
                f[j][i] = f[j - 1][i];
                if (i >= A[j - 1] && f[j - 1][i - A[j - 1]]) {
                    f[j][i] = true;
                }
            }
        }

        for (int i = m; i >= 0; i--) {
                if (f[A.length][i]) {
                   return i;
                }
            }
        return 0;
    }

 用 integer 来替代 

public int backPack(int m, int[] A) {
        // write your code here
        int[][] f = new int[A.length + 1][m + 1];

        //initialize
        f[0][0] = 0;
        for (int i = 1; i <= m; i++) {
            f[0][i] = Integer.MIN_VALUE;
        }

        for (int i = 1; i <= A.length; i++) {
            f[i][0] = 0;
        }
        int ans = 0;
        //function
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= A.length; j++) {
                f[j][i] = f[j - 1][i];
                if (i >= A[j - 1] && f[j - 1][i - A[j - 1]] != Integer.MIN_VALUE) {
                    f[j][i] = Math.max(f[j][i], f[j - 1][i - A[j - 1]] + A[j - 1]);
                    ans = Math.max(ans, f[j][i]);
                }

            }
        }
        return ans;
}

  

时间: 2024-08-12 01:06:07

Backpack的相关文章

Backpack II

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? 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. 这是最经典的0-1背

Lintcode: Backpack

Given n items with size A[i], an integer m denotes the size of a backpack. How full you can fill this backpack? Note You can not divide any item into small pieces. Example If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can sel

BackPack 2

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? Notice You cannot divide item into small pieces and the total size of items you choose should smaller or equal to m. Have you

LeetCode Backpack

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack? 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 b

[LintCode] BackPack Dynamic Programming Problems

This blog talks about using dynamic programming to solve the famous back pack and its variant problems. BackPack I Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack? Notice You can not divide

[LintCode] Backpack VI 背包之六

Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Notice The different sequences are counted as different combinations. Have you met this questi

Backpack IV

Description Given an integer array nums[] which contains n unique positive numbers, num[i] indicate the size of ith item. An integer target denotes the size of backpack. Find the number of ways to fill the backpack. Each item may be chosen unlimited

Backpack V

Description Given n items with size nums[i] which an integer array and all positive numbers. An integer target denotes the size of a backpack. Find the number of possible fill the backpack. Each item may only be used once Example Given candidate item

Backpack III

Description Given n kinds of items, and each kind of item has an infinite number available. The i-th item has size A[i] and value V[i]. Also given a backpack with size m. What is the maximum value you can put into the backpack? You cannot divide item