2015年9月27日星期日

Lintcode: Backpack

Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack?
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.
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.
Reference:
http://www.code123.cc/docs/leetcode-notes/dynamic_programming/backpack.html

s1: D[i][j]:  bool array ---- whether the amount j can be filled with the beginning i items;
s2: If not put current item into backpack: D[i][j] = D[i-1][j]
      if put it into backpack: D[i][j] = D[i-1][j - A[i]]
s3: Initial condition: 0 <= i <= A.size()    0 <= j <= m
      D[0][0] = 1;  D[0][j] = 0; D[i][0] = 1;

Time: O(m*n)
Space: O(m*n)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public:
    /**
     * @param m: An integer m denotes the size of a backpack
     * @param A: Given n items with size A[i]
     * @return: The maximum size
     */
    int backPack(int m, vector<int> A) {
        // write your code here
        vector<vector<bool> > dp(A.size() + 1,vector<bool>(m + 1, false));
        dp[0][0] = true;
        for(int i = 1; i <= A.size(); i++){
            dp[i][0] = true;
        }
        for(int j = 1; j <= m; j++){
            dp[0][j] = false;
        }
        for(int i = 1; i <= A.size(); i++){
            for(int j = 1; j <= m; j++){
                
                if(j < A[i - 1]) dp[i][j] = dp[i - 1][j];
                else dp[i][j] = dp[i - 1][j] || dp[i - 1][j - A[i - 1]];
                //anotation part are supposed to be optimization
                //however, time limit when test, strange
                /*
                dp[i][j] = dp[i - 1][j];
                if((j >= A[i - 1]) && (dp[i - 1][j - A[i - 1]] == true)){
                    dp[i][j] = dp[i - 1][j - A[i - 1]];
                }*/
            }
        }
        for(int j = m; j >= 0; j--){
            if(dp[A.size()][j] == true) return j;
        }
    }
};

Optimization of the space:
Time: O(m*n)
Space: O(m)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
    /**
     * @param m: An integer m denotes the size of a backpack
     * @param A: Given n items with size A[i]
     * @return: The maximum size
     */
    int backPack(int m, vector<int> A) {
        // write your code here
        vector<bool>  res(m + 1, false);
        res[0]= true; 
        
        for(int i = 1; i <= A.size(); i++){
            for(int j = m; j >= 1; j--){
                /*
                if(j < A[i - 1]) res[j] = res[j];
                else{
                    res[j] = res[j] || res[j - A[i - 1]];
                }*/
                //simplify
                if(j >= A[i - 1] && res[j - A[i - 1]]) res[j] = true;
            }
        }
        for(int j = m; j >= 0; j--){
            if(res[j] == true) return j;
        }
    }
};

没有评论:

发表评论