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]
相关推荐
Wect39 分钟前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·算法·typescript
糖果店的幽灵43 分钟前
决策树详解与sklearn实战
算法·决策树·sklearn
Lewiis1 小时前
趣谈排序算法
算法·排序算法
ComputerInBook1 小时前
数字图像处理(4版)——第 8 章——图像压缩与水印(上)(Rafael C.Gonzalez&Richard E. Woods)
人工智能·算法·计算机视觉·图像压缩·图像水印
刀法如飞1 小时前
Python列表去重:从新手三连到高阶特技,20种解法全收录
python·算法·编程语言
minji...1 小时前
算法题 动态规划
算法·动态规划
水蓝烟雨2 小时前
3337. 字符串转换后的长度 II
算法·leetcode
MegaDataFlowers2 小时前
SiliconCompiler workflow
算法
_日拱一卒2 小时前
LeetCode:226翻转二叉树
数据结构·算法·leetcode
踩坑记录2 小时前
leetcode hot100 64. 最小路径和 medium 递归优化
leetcode·深度优先