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]
相关推荐
Allen_LVyingbo13 小时前
量子测量三部曲:投影测量、POVM 与坍缩之谜—从形式主义到物理图像
算法·性能优化·健康医疗·量子计算·空间计算
qiqsevenqiqiqiqi13 小时前
位运算 计算
算法
甄心爱学习13 小时前
【最优化】1-6章习题
人工智能·算法
PD我是你的真爱粉13 小时前
向量数据库原理与检索算法入门:ANN、HNSW、LSH、PQ 与相似度计算
数据库·人工智能·算法
汀、人工智能13 小时前
[特殊字符] 第72课:杨辉三角
数据结构·算法·数据库架构·图论·bfs·杨辉三角
_深海凉_14 小时前
LeetCode热题100- 字母异位词分组
leetcode
洛水水14 小时前
【力扣100题】14.两数相加
c++·算法·leetcode
我不是小upper14 小时前
相关≠因果!机器学习中皮尔逊相关检验的完整流程
人工智能·算法·机器学习
float_com14 小时前
LeetCode80. 删除有序数组中的重复项 II
leetcode
pwn蒸鱼14 小时前
leetcode:21. 合并两个有序链表
算法·leetcode·链表