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]
相关推荐
计算机安禾12 小时前
【C语言程序设计】第27篇:递归函数原理与实例分析
c语言·开发语言·数据结构·c++·算法·蓝桥杯·visual studio
無限進步D12 小时前
C++ 万能头
开发语言·c++·算法·蓝桥杯·竞赛·万能头
qq_4181017712 小时前
C++中的状态模式
开发语言·c++·算法
weixin_3077791312 小时前
构建健壮的XML文档抓取与摘要流水线:Requests + urllib3.Retry + lxml 实践
xml·开发语言·python·算法·性能优化
如何原谅奋力过但无声12 小时前
【力扣-Python-74】搜索二维矩阵(middle)
数据结构·python·算法·leetcode·矩阵
圣保罗的大教堂12 小时前
leetcode 1878. 矩阵中最大的三个菱形和 中等
leetcode
咱就是说不配啊12 小时前
3.16打卡day30
数据结构·c++·算法
MicroTech202512 小时前
MLGO微算法科技面向复杂非局域模型的量子虚时演化新方案:一种无需局域性假设的量子虚时演化新算法
科技·算法·量子计算
weixin_6495556712 小时前
C语言程序设计第四版(何钦铭、颜晖)第八章指针之判断回文字符串
c语言·开发语言·算法
luckycoding12 小时前
3392. 统计符合条件长度为 3 的子数组数目
数据结构·算法·leetcode