coinsChange
by sandfresh
JavaScript
function coinsChange(coins,amount){
let dp = new Array(amount+1);
dp.fill(amount+1);
dp[0] = 0;
for(let i = 0 ; i <= amount ; i++)
for(let j = 0 ; j< coins.length; j++){
if(coins[j] <= i){
dp[i] = Math.min(dp[i],dp[i - coins[j]]+1);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
let coins = [1, 2, 5];
let amount = 11
let ret = coinsChange(coins,amount);
console.log(ret);