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]
相关推荐
卷福同学9 小时前
去掉手机APP开屏广告,李跳跳2.2下载使用
java·后端·算法
漫霂9 小时前
二叉树的翻转
java·数据结构·算法
语戚9 小时前
力扣 51. N 皇后:基础回溯、布尔数组优化、位运算全解(Java 实现)
java·算法·leetcode·力扣·剪枝·回溯·位运算
熊猫钓鱼>_>9 小时前
从零构建大模型可调用的Skill:基于Function Calling的完整指南
人工智能·算法·语言模型·架构·agent·skill·functioncall
py有趣9 小时前
力扣热门100题之螺旋矩阵
算法·leetcode
xiaoyaohou1110 小时前
003、轻量化改进(一):网络剪枝原理与实战
算法·机器学习·剪枝
我是章汕呐10 小时前
政策评估的“黄金标准”:DID模型从原理到Stata实操
大数据·人工智能·经验分享·算法·回归
2301_8227032010 小时前
光影进度条:鸿蒙Flutter实现动态光影效果的进度条
算法·flutter·华为·信息可视化·开源·harmonyos
人道领域10 小时前
【LeetCode刷题日记】383 赎金信
算法·leetcode·职场和发展
炽烈小老头10 小时前
【每天学习一点算法 2026/04/11】Pow(x, n)
学习·算法