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]
相关推荐
free-elcmacom2 分钟前
C++<x>new和delete
开发语言·c++·算法
lxh01134 分钟前
计算右侧小于当前元素的个数 题解
javascript·数据结构·算法
滴滴答滴答答7 分钟前
机考刷题之 12 LeetCode 684 冗余的边
算法·leetcode·职场和发展
美式请加冰17 分钟前
前缀数组的介绍和使用
数据结构·c++·算法
codeyanwu21 分钟前
LeetCode Hot 100 -- 图论
leetcode·深度优先·图论
IronMurphy23 分钟前
【算法十九】33. 搜索旋转排序数组 74. 搜索二维矩阵
线性代数·算法·矩阵
Srend66627 分钟前
【图论】最短路问题
算法·图论
云泽80832 分钟前
蓝桥杯算法精讲:二分算法之二分查找深度剖析
算法·职场和发展·蓝桥杯
电报号dapp11933 分钟前
公链浏览器:区块链世界的“数据透视镜”与哈希查询的艺术
算法·区块链·智能合约·哈希算法
phltxy37 分钟前
前缀和算法:从一维到二维,解锁高效区间求和
java·开发语言·算法