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

322. 零钱兑换 #55

Open
Geekhyt opened this issue Apr 26, 2021 · 0 comments
Open

322. 零钱兑换 #55

Geekhyt opened this issue Apr 26, 2021 · 0 comments
Labels

Comments

@Geekhyt
Copy link
Owner

Geekhyt commented Apr 26, 2021

原题链接

题目仅要求出最少硬币数量,无须考虑硬币的组合和排列,所以不用考虑两个 for 循环的内外顺序。

状态定义

dp[i]:凑足总额为 i 所需钱币的最少个数为 dp[i]

状态转移方程

dp[i] = min(dp[j - coins[j]] + 1, dp[i])

理解

不考虑第 j 个硬币时, 硬币数为 dp[i]

考虑第 j 个硬币时,硬币数为 dp[i - coins[j]] + 1

初始化

凑足总金额为 0 所需钱币的个数是 0,所以 dp[0] = 0

下标非 0 元素应该为最大值: Number.MAX_VALUE

const coinChange = function (coins, amount) {
    const dp = Array(amount + 1).fill(Number.MAX_VALUE)
    dp[0] = 0
    for (let i = 1; i < dp.length; i++) {
        for (let j = 0; j < coins.length; j++) {
            if (i - coins[j] >= 0) {
                dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1)
            }
        }
    }
    return dp[dp.length - 1] === Number.MAX_VALUE ? -1 : dp[dp.length - 1]
}
  • 时间复杂度: O(len(coins) * amount)
  • 空间复杂度: O(amount)
@Geekhyt Geekhyt added the 中等 label Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant