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]
相关推荐
佑白雪乐7 分钟前
LCR 175. 计算二叉树的深度
算法·深度优先
阿Y加油吧14 分钟前
力扣打卡day07——最大子数组和、合并区间
算法
想吃火锅100518 分钟前
【leetcode】105. 从前序与中序遍历序列构造二叉树
算法·leetcode·职场和发展
圣保罗的大教堂20 分钟前
leetcode 3567. 子矩阵的最小绝对差 中等
leetcode
2401_8318249625 分钟前
嵌入式C++驱动开发
开发语言·c++·算法
靠沿27 分钟前
【优选算法】专题十八——BFS解决拓扑排序问题
算法·宽度优先
cui_ruicheng30 分钟前
C++数据结构进阶:哈希表实现
数据结构·c++·算法·哈希算法·散列表
li星野41 分钟前
[特殊字符] 模拟试卷一:C++核心与系统基础(90分钟)答案版
开发语言·c++·算法
二进制星轨1 小时前
leecode-283-移动零-算法题解
算法
老鼠只爱大米1 小时前
LeetCode经典算法面试题 #215:数组中的第K个最大元素(快速选择、堆排序、计数排序等多种实现方案详解)
算法·leetcode·堆排序·快速选择·topk·数组中的第k个最大元素