思路:
很经典的动态规划题目。我们只需要写出递推公式:num(i) = min( num( i - coin( j ) ) ) +1. coin( j ) 为硬币的面额。
class Solution {
public int coinChange(int[] coins, int amount) {
int[] res = new int[amount+1];
res[0] = 0;
for(int i=1;i<amount+1;i++)
{
res[i] = -1;
int min = i;
for(int j=0;j<coins.length;j++)
{
if(i>=coins[j])
{
if(res[i-coins[j]]!=-1)
{
min = Math.min(min,1+res[i-coins[j]]);
res[i] = min;
}
}
}
}
return res[amount];
}
}