Backpack Problems

1. Backpack I

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

(http://www.lintcode.com/en/problem/backpack/)

1) brute-force: DFS

2) Whether we put the last item? (assuming n items)

  No: the maximum to m is the previous n-1 items 

  Yes: the maximum to m - A[n] for previous n -1 items

 1 public class Solution {
 2     /**
 3      * @param m: An integer m denotes the size of a backpack
 4      * @param A: Given n items with size A[i]
 5      * @return: The maximum size
 6      */
 7     public int backPack(int m, int[] A) {
 8         // write your code here
 9         //the maximum when trying to put first n items into m capacity backpack 
10         //f[n][m] = max(f[n-1][m], f[n-1][m-A[n]]+A[n])
11         //f[0][0] = 0, f[0][1] = 0 ...f[0][m]=0
12         
13         int n = A.length;
14         
15         int[][] dp = new int[n+1][m+1];
16         
17         for (int i = 1; i <= n; i++) {
18             
19             for (int j = 1; j <= m; j++) {
20                 dp[i][j] =  dp[i-1][j];
21                 
22                 if ( j >= A[i-1]) {
23                     dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-A[i-1]]+A[i-1]);
24                 }
25             }
26         }
27         
28         return dp[n][m];
29     }
30 }

 

Optimization by using rolling array

 1 //Optimization by using rolling array 
 2      public int backPack(int m, int[] A) {
 3         // write your code here
 4         // everytime we update dp, only use the last dp row
 5         // eg. dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j-A[i-1]]+A[i-1]) only related to dp[i-1][...]
 6         
 7         int n = A.length;
 8         
 9         int[] dp = new int[m+1];
10         
11         for (int i = 1; i <= n; i++) {
12             
13             // why we have to use descending order?
14             // cause we have to update dp[j] before dp[j-A[i-1]], otherwise, when updating dp[j], we will use the updated/polluted dp[j-A[i-1]] 
15             for (int j = m; j >= A[i-1]; j--) {
16                 
17                 dp[j] = Math.max(dp[j], dp[j - A[i-1]] + A[i-1]);
18                 
19             }
20         }
21         
22         return dp[m];
23      }

 

2. 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?

http://www.lintcode.com/en/problem/backpack-ii/

f[n][m] = max(f[n-1][m], f[n-1][m-A[n]]+V[n])

Normal 2-d array

[TO DO]

 

Optimization by using rolling array

 1 public class Solution {
 2     /**
 3      * @param m: An integer m denotes the size of a backpack
 4      * @param A & V: Given n items with size A[i] and value V[i]
 5      * @return: The maximum value
 6      */
 7     public int backPackII(int m, int[] A, int V[]) {
 8         // write your code here
 9         int n = A.length;
10          
11         int[] dp = new int[m+1];
12         
13         for (int i = 1; i <= n; i++) {
14            
15             for (int j = m; j >= A[i-1]; j--) {
16                 // only replace last A[i-1] with V[i-1]
17                 dp[j] = Math.max(dp[j], dp[j-A[i-1]]+ V[i-1]);
18             }
19          }
20          
21         return dp[m];
22     }
23 }

 

3. Backpack III 

What if each item has unlimited numbers?

method 1: f[n][m] = max(f[n-1][m], f[n-1][m- k*A[n]]+ k*V[n]) ,  m-k*A[n] >= 0

method 2: f[n][m] = max(f[n-1][m], f[n][m- A[n]]+ V[n]) 

Normal 2-D array

[TO DO]

Optimization

 1 public class Solution {
 2     /**
 3      * @param m: An integer m denotes the size of a backpack
 4      * @param A & V: Given n items with size A[i] and value V[i]
 5      * @return: The maximum value
 6      */
 7     public int backPackII(int m, int[] A, int V[]) {
 8         // write your code here
 9         int n = A.length;
10          
11          int[] dp = new int[m+1];
12         
13         for (int i = 1; i <= n; i++) {
14             // using asc order
15             for (int j = A[i-1]; j <= m; j++) {
16                 dp[j] = Math.max(dp[j], dp[j-A[i-1]]+ V[i-1]);
17             }
18          }
19          
20         return dp[m];
21     }
22 }

 

4. Backpack IV

Each item only has one. How many ways to fill up the bag?

[TO DO]

 

5. Backpack V

Each item has umlimited numbers. How many ways to fill up the bag?

https://leetcode.com/problems/combination-sum-iv/

Note: The combination sum IV in leetcode shares the same DP model and solution with this kind of Backpack problem. 

public class Solution {
    public int combinationSum4(int[] nums, int target) {
        
        int[] dp = new int[target+1];
        
        // how to undestand dp[0] = 1?
        // if target is 0, only 1 way - nothing to add
        dp[0] = 1; 
        Arrays.sort(nums);
        
        for (int i =1; i <= target; i++) {
            for (int num : nums) {
                
                if (i < num) break;
                dp[i] = dp[i] + dp[i-num];
            }
        }
        
        return dp[target];
        
        
    }
}

 

posted on 2016-10-13 11:10  kkkiii  阅读(178)  评论(0)    收藏  举报