有限制的零钱兑换
public class coin_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] money= {1,5,10,50,100};
int[] num= {5, 2 ,2, 3 ,5};
System.out.println(solve(money,num,47));
}
public static int solve(int[] money,int[] num,int aim) {
int[][] dp=new int[money.length+1][aim+1];
for(int i=0;i<=money.length;i++)
for(int j=1;j<=aim;j++)
dp[i][j]=aim+1;
for(int i=1;i<=money.length;i++)
for(int j=1;j<=aim;j++) {
dp[i][j]=dp[i-1][j];
int kv=Math.min(j/money[i-1], num[i-1]);
for(int k=1;k<=kv;k++) {
dp[i][j]=Math.min(dp[i][j], dp[i-1][j-k*money[i-1]]+k);
}
}
return dp[money.length][aim]>aim?-1:dp[money.length][aim];
}
}