Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

零钱兑换 #451

Open
Stream-web opened this issue Feb 4, 2023 · 0 comments
Open

零钱兑换 #451

Stream-web opened this issue Feb 4, 2023 · 0 comments

Comments

@Stream-web
Copy link
Contributor

题目描述:
给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额。

计算并返回可以凑成总金额所需的 最少的硬币个数 。如果没有任何一种硬币组合能组成总金额,返回 -1 。

你可以认为每种硬币的数量是无限的。
示例代码:

/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
var coinChange = function(coins, amount) {
     let dp = new Array(amount + 1).fill(Infinity);

     dp[0] = 0;
     for(let i =1;i<=amount;i++){
        for(const coin of coins) {
            if(i - coin >=0){
                dp[i] = Math.min(dp[i],dp[i-coin]+1)
            }
        }
     }
     return dp[amount] === Infinity ? -1 : dp[amount];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant