1 class Solution {
 2 public:
 3     /**
 4      * @param m: An integer m denotes the size of a backpack
 5      * @param A: Given n items with size A[i]
 6      * @return: The maximum size
 7      */
 8     int backPack(int m, vector<int> A) {
 9         // write your code here
10         vector<int> result(m+1, 0);
11         for (int i = 0; i < A.size(); i++) {
12             for (int j = m; j >= A[i]; j--) {
13                 result[j] = max(result[j], result[j-A[i]] + A[i]);
14             }
15         }
16         return result[m];
17     }
18 };

 

posted on 2015-04-06 13:23  keepshuatishuati  阅读(131)  评论(0编辑  收藏  举报