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]
相关推荐
小O的算法实验室2 分钟前
2026年IEEE TBD,面向大规模优化的随机矩阵粒子群算法,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
哭泣方源炼蛊2 分钟前
AtCoder Beginner Contest 456 E补题(分层图 + 有向环检测 )
c++·算法·深度优先·图论·拓扑学
平行侠16 分钟前
022Miller-Rabin 概率素性检验 - 概率与数论的完美联姻
数据结构·算法
wuweijianlove30 分钟前
算法与数据结构协同优化的设计思想的技术7
数据结构·算法
昵称小白33 分钟前
二叉树专题(下)
算法·深度优先
故事和你9135 分钟前
洛谷-数据结构2-1-二叉堆与树状数组1
开发语言·数据结构·c++·算法·动态规划·图论
多加点辣也没关系1 小时前
数据结构与算法|第十七章:贪心算法
数据结构·算法·贪心算法
多加点辣也没关系1 小时前
数据结构与算法|第十四章:排序算法(上)— 比较类排序
数据结构·算法·排序算法
笨笨饿1 小时前
#72_聊聊I2C以及他们的变体
linux·c语言·网络·stm32·单片机·算法·个人开发
机器人图像处理1 小时前
6-自动白平衡(灰度世界算法)
opencv·算法·相机