Leetcode 518. Coin Change II

Problem

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.

You may assume that you have an infinite number of each kind of coin.

The answer is guaranteed to fit into a signed 32-bit integer.

Algorithm

Dynamic Programming (DP). Complete knapsack problem, forward to sum the ways.

Code

python3 复制代码
class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        dp = [0] * (amount + 1)
        dp[0] = 1
        for coin in coins:
            for v in range(coin, amount+1):
                dp[v] += dp[v - coin]
        
        return dp[amount]
相关推荐
Bear on Toilet12 分钟前
递归_二叉树_50 . 从前序与中序遍历序列构造二叉树
数据结构·算法·leetcode·深度优先·递归
plus4s14 分钟前
2月18日(82-84题)
c++·算法·动态规划
艾醒1 小时前
打破信息差——2026年2月19日AI热点新闻速览
算法
追随者永远是胜利者2 小时前
(LeetCode-Hot100)62. 不同路径
java·算法·leetcode·职场和发展·go
追随者永远是胜利者2 小时前
(LeetCode-Hot100)56. 合并区间
java·算法·leetcode·职场和发展·go
wu_asia3 小时前
每日一练伍
算法
追随者永远是胜利者3 小时前
(LeetCode-Hot100)55. 跳跃游戏
java·算法·leetcode·游戏·go
近津薪荼3 小时前
优选算法——前缀和(7):连续数组
算法
ArturiaZ3 小时前
【day29】
数据结构·c++·算法
MoonOutCloudBack4 小时前
VeRL 框架下 RL 微调 DeepSeek-7B,比较 PPO / GRPO 脚本的参数差异
人工智能·深度学习·算法·语言模型·自然语言处理