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]
相关推荐
程序员清风2 小时前
算法题中的边界条件、测试与调试
开发语言·算法·kotlin
炘爚3 小时前
数据结构算法总结
数据结构·python·算法
Dageno_AI3 小时前
ChatGPT 不再引用 Reddit 之后,还值得在 Reddit 上做 AI 可见度吗?
算法
hetao17338373 小时前
2026-10-25 hetao1733837 的刷题记录
c++·算法
Jsundoku4 小时前
第七章 机器学习 -- 其他经典算法
人工智能·算法·机器学习
进击的丸子4 小时前
基于 ArcFace SDK 的跨平台人脸特征值下发、同步以及存储方案
后端·算法·代码规范
见叶之秋4 小时前
【C++】红与黑的边界:红黑树从插入到自检
数据结构·c++·算法·面试
一条闲鱼_mytube5 小时前
分布式 ID 生成方案:雪花算法与号段模式
分布式·算法·wpf
Msir_Modern5 小时前
Noita 元胞沙盒笔记
算法·unity·c#·游戏引擎·元胞自动机
木井巳6 小时前
【动态规划: 路径问题】不同路径
java·算法·leetcode·动态规划