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]
相关推荐
奋发向前wcx4 小时前
CSP-J复赛模拟赛4 王晨旭补题 2026.10.5
算法
念何架构之路7 小时前
zap采样器与性能优化内幕
算法·性能优化·哈希算法
晒太羊的猫8 小时前
hot100动态规划刷题
算法·动态规划
weixin_531094698 小时前
heap_4内存管理
java·开发语言·算法
余额瞒着我当琳8 小时前
红黑树的实现--红黑树的概念、规则、效率、结构、旋转、查找、验证
数据结构·c++·算法
程序员南飞8 小时前
win系统安装minio
java·数据结构·算法
青少儿编程课堂8 小时前
AC自动机多模式串匹配精讲:校园关键词巡检实战解析
c++·python·算法·bfs·信息学竞赛
longlongzihan9 小时前
LeetCode 5. 最长回文子串:从暴力匹配到中心扩展的思维演进
c++·算法·leetcode
waoooqwe9 小时前
问卷样本真实吗
大数据·数据库·算法·数据分析
苦瓜打怪兽9 小时前
leetcode 热题100(11. 盛最多水的容器)
python·算法·leetcode