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]
相关推荐
AbandonForce12 分钟前
简谈线程池
开发语言·c++·算法
智购科技智能售货柜22 分钟前
2026自动售货机商品掉落声学计数方案:从麦克风到频谱识别的工程实践~YH
人工智能·算法
土司大王35 分钟前
LeetCode hot100——实现 Trie (前缀树)
java·算法·leetcode
水龙吟啸40 分钟前
华为研发岗AI方向9.9机考题复盘&分析
人工智能·python·算法·华为
小七在进步42 分钟前
类和对象(一)
java·数据结构·算法
Y_Bk43 分钟前
2026 ICPC EC网络预选赛第一场
算法
CarIise1 小时前
C语言字符串基础:从char数组到双指针反转算法
算法
佳児素花痴╮1 小时前
C++速通2
开发语言·c++·算法
麻瓜code1 小时前
【LeetCode】相交链表:双指针法,一次遍历找到交点
算法·leetcode·链表
zander2582 小时前
LeetCode 5. 最长回文子串
算法