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]
相关推荐
脱氧核糖核酸__4 分钟前
LeetCode热题100——48.旋转图像(题解+答案+要点)
c++·算法·leetcode
木井巳6 分钟前
【递归算法】字母大小写全排列
java·算法·leetcode·决策树·深度优先
宵时待雨7 分钟前
优选算法专题2:滑动窗口
数据结构·c++·笔记·算法
Mr_pyx9 分钟前
LeetCode HOT 100 —— 矩阵置零(多种解法详解)
算法·leetcode·矩阵
葫三生10 分钟前
《论三生原理》系列:文化自信、知识范式重构与科技自主创新的思想运动源头?
大数据·人工智能·科技·深度学习·算法·重构·transformer
Q741_14713 分钟前
每日一题 力扣 3761. 镜像对之间最小绝对距离 哈希表 数组 C++ 题解
c++·算法·leetcode·哈希算法·散列表
John.Lewis14 分钟前
C++加餐课-哈希:扩展学习(2)布隆过滤器
c++·算法·哈希算法
我真不是小鱼29 分钟前
cpp刷题打卡记录29——矩阵置零 & 旋转图像 & 除了自身以外数组的乘积
数据结构·c++·算法·leetcode·矩阵
澈20742 分钟前
快速排序与希尔排序实战解析
数据结构·算法·排序算法
帅小伙―苏1 小时前
力扣128.最长连续序列
算法·leetcode