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]
相关推荐
weixin_307779137 分钟前
Python用正则把 SQL 关键字整词匹配后转成小写
开发语言·python·sql·算法
Code_Solitude1 小时前
c语言:期末复习5(二维数组)
c语言·数据结构·算法
CV爱数码1 小时前
论文精读|DC-PnPDP(ICML 2026):给即插即用扩散先验加回 ADMM 的对偶变量
人工智能·深度学习·算法·3d·ct
电科_银尘1 小时前
【大模型算法与工程 52】检索优化:混合召回、重排与查询改写
人工智能·算法·机器学习
猿人谷1 小时前
Pareto:为什么真正的优化,不是寻找一个最优解?
后端·算法·aigc
cooldream20091 小时前
计算机专业本科毕业设计系列07——类型④:算法与理论研究
算法·毕业设计·计算机专业·课程设计
劲夫学编程2 小时前
leetcode: 字母异位词分组
算法·leetcode
wy_hhxx2 小时前
[Leetcode] 动态规划(打家劫舍、0-1背包、完全背包)
算法·leetcode·动态规划·完全背包·0-1背包
m0_547486663 小时前
《智能计算》全套课件PDF(武汉工程大学)
算法·贝叶斯算法·智能计算
Shaoshing3 小时前
LeetCode100 32.最长有效括号
java·数据结构·leetcode