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]
相关推荐
hanlin0311 小时前
刷题笔记:力扣第32题-最长有效括号
笔记·算法·leetcode
All for pursuit.11 小时前
【动态规划-21】312.戳气球
数据结构·c++·算法·leetcode·动态规划
碧海银沙音频科技研究院14 小时前
基于炬芯ATS3089X自研AEC声学回声消除算法
算法
通问AI14 小时前
在两台 Mac Studio 上跑 800B 模型:MLX + RDMA over Thunderbolt 实测与四道门选型法
算法
ZywOo62314 小时前
《算法竞赛进阶指南》0x03 前缀和与差分
算法
Scintilla_y14 小时前
DAY2 滑动窗口
算法·leetcode·散列表
lvwangshu14 小时前
笛卡尔树
算法·图论
026d2315 小时前
状态信号只知“其然”不知“其所以然”:变工况机械故障诊断的物理盲区
算法
姜鱼问生15 小时前
备份和服务器在同一台机器上,等于没备份
算法·架构
姜鱼问生15 小时前
别再 chmod 777 了,用 ACL 给它开一扇窗
后端·算法·架构