92 · 背包问题Backpack
Given n items with size A_{i}A i an integer m denotes the size of a backpack. How full you can fill this backpack?
Input:
array = [3,4,8,5]
backpack size = 10
Output:
9
Explanation:
Load 4 and 5.
public class Solution {
/**
* @param m: An integer m denotes the size of a backpack
* @param A: Given n items with size A[i]
* @return: The maximum size
*/
public int backPack(int m, int[] A) {
// write your code here
int n = A.length ;
boolean[][] f = new boolean[n+1][m+1] ;
for(int i = 0 ; i <= n ; i++){
f[i][0] = true ;
}
for(int i = 1 ; i <= m ; i++){
f[0][i] = false ;
}
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
f[i][j] = f[i-1][j] ;
if(j >= A[i-1] && f[i-1][j-A[i-1]]){
f[i][j] = true ;
}
}
}
for(int i = m ; i >= 0 ; i--){
if(f[A.length][i]){
return i ;
}
}
return 0 ;
}
}