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]
相关推荐
threerocks42 分钟前
Jev 入门第一课
算法
西柚研究生1234562 小时前
论文分析17:YOLOv11_UAVNet:无人机航拍图像专用目标检测算法
人工智能·python·深度学习·算法·目标检测
hetao17338372 小时前
2026-09-17 hetao1733837 的刷题记录
c++·算法
午彦琳3 小时前
2026.9.17
数据结构·算法·leetcode
木井巳3 小时前
【记忆化搜索】不同路径
java·算法·leetcode·深度优先·剪枝·推荐算法
怕浪猫5 小时前
从 Windows 换到 Mac 三个月,我真香了
算法·面试·架构
aichitang20245 小时前
前端小skill
前端·人工智能·算法·ai·前端框架
All for pursuit.6 小时前
【链表-9】146.LRU缓存
数据结构·c++·算法·leetcode
木子算法6 小时前
测出来的值会抖:约束和目标带噪声时,「可行」和「更好」该怎么判
人工智能·算法·目标跟踪
圣保罗的大教堂7 小时前
leetcode 1477. 找两个和为目标值且不重叠的子数组 中等
leetcode