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]
相关推荐
longlongzihan31 分钟前
LeetCode 17电话号码的字母组合:回溯算法(DFS)详解
c++·算法·leetcode·深度优先
封印师请假去地球钓鱼38 分钟前
边解边变的问题:从“决策依赖“一词出发
人工智能·算法
今晚打老虎1 小时前
c++之提高A(前缀和)(第三课)
数据结构·c++·算法
不会就选b3 小时前
算法日常・每日刷题--<贪心>26
数据结构·算法
hetao17338374 小时前
2026-09-29 hetao1733837 的刷题记录
c++·算法
欣欣之王来了5 小时前
AI合规专项:AI算法透明度的合规要求
人工智能·算法
wzdark5 小时前
分块思想在算法优化中的实践与应用4
算法
Because_of_Her15 小时前
dfs-洪水填充技巧
笔记·算法·深度优先·洪水填充
小静AI工程实验室5 小时前
RSA 算法详解:从模幂原理到 OAEP 加密、PSS 签名与 Python 实验
python·算法
xzal125 小时前
GESP Python笔记
笔记·python·算法