装箱问题
https://www.nowcoder.com/practice/d195a735f05b46cf8f210c4ad250681c
import java.util.*;
/**
* NC320 装箱问题
* @author d3y1
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param V int整型
* @param num int整型ArrayList
* @return int整型
*/
public int boxin (int V, ArrayList<Integer> num) {
return solution(V, num);
}
/**
* 动态规划: 01背包
*
* dp[j]表示箱子容量为j时可以装箱的物品最大体积(每个物品只能使用一次!)
*
* dp[j] = Math.max(dp[j], dp[j-vol]+vol)
*
* @param V
* @param num
* @return
*/
private int solution(int V, ArrayList<Integer> num){
int n = num.size();
int[] dp = new int[V+1];
int vol;
for(int i=0; i<n; i++){
// 每个物品只能使用一次! => 倒序
for(int j=V; j>0; j--){
vol = num.get(i);
if(j >= vol){
dp[j] = Math.max(dp[j], dp[j-vol]+vol);
}
}
}
return V-dp[V];
}
}