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]
相关推荐
比昨天多敲两行3 分钟前
C++ Lsit
开发语言·c++·算法
我爱C编程4 分钟前
基于OMP正交匹配追踪和稀疏字典构造的杂波谱恢复算法matlab仿真
算法·matlab·omp·正交匹配追踪·稀疏字典构造·杂波谱恢复
云青黛6 分钟前
ReAct(推理与行动)框架
python·算法
im_AMBER17 分钟前
Leetcode 142 将有序数组转换为二叉搜索树 | 排序链表
算法·leetcode
码农三叔18 分钟前
(10-5-01)大模型时代的人形机器人感知:基于RoboBrain大模型的人形机器人通用智能感知系统(1)构建模型
人工智能·算法·机器人·人形机器人
scott19851218 分钟前
扩散模型之(十三)条件生成 Conditioned Generation
人工智能·算法·生成式
Wect29 分钟前
LeetCode 53. 最大子数组和:两种高效解法(动态规划+分治)
前端·算法·typescript
春日见35 分钟前
端到端自动驾驶综述
linux·人工智能·算法·机器学习·自动驾驶
Book思议-1 小时前
【数据结构实战】单向循环单链表判别条件理解
c语言·数据结构·算法
逆境不可逃1 小时前
【后端新手谈 04】Spring 依赖注入所有方式 + 构造器注入成官方推荐的原因
java·开发语言·spring boot·后端·算法·spring·注入方式