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 小时前
在PC电脑上用Whisper→Marian算法实现离线语音翻译功能
算法·whisper
niyongsheng2 小时前
3D 装箱算法工程化:数据结构、放置启发式与支撑规则
数据结构·算法·rust
j7~5 小时前
【穷举、深搜与回溯算法】一棵决策树讲透全排列与子集
数据结构·c++·算法·leetcode·回溯·递归
傅科摆 _ py6 小时前
DP,贪心,堆和栈,二分力扣复习
开发语言·python·算法
Shaoshing6 小时前
LeetCode100 42.接雨水
java·算法·leetcode
域限奇点访客6 小时前
用AI生成问卷的网站怎么选?看题目录入结构、题型映射和生成后的校验
算法
文丑颜不良啊7 小时前
LeetCode1261~1280题目解析
java·leetcode
not coder7 小时前
一文讲通--从树到红黑树
数据结构·算法
octopus_c7 小时前
C++ 入门
开发语言·c++·算法
ZhengEnCi8 小时前
2026年10月30日AI早报:Gemini 4 Carbon登顶、DeepSeek融资1055亿、智元机器人上岗实测
人工智能·算法