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]
相关推荐
小小仙子35 分钟前
矢量网络分析仪如何测试S参数的?
人工智能·算法·机器学习
中微极客2 小时前
降维算法75倍加速:从PCA到稀疏字典学习的工程实践
人工智能·学习·算法
storyseek3 小时前
前缀和实现Kogge-Stone算法
数据结构·算法
元Y亨H3 小时前
开发者必须掌握的十大核心算法
算法
元Y亨H3 小时前
深度解构:数据结构与算法的理论基石与工程演进
数据结构·算法
元Y亨H3 小时前
数据结构与算法的通俗指南
数据结构·算法
2301_764441333 小时前
用动力学系统(微分方程)为 Kernberg 的客体关系单元提供数学化的操作定义,把“自体—客体“这对心理结构建模成一个二维耦合系统
数据结构·python·算法·数学建模
林泽毅4 小时前
PyTRIO快速入门(二):Datum构建
人工智能·算法·产品
keep intensify4 小时前
最长有效括号
算法·leetcode·动态规划
CoderYanger4 小时前
A.每日一题:1979. 找出数组的最大公约数
java·程序人生·算法·leetcode·面试·职场和发展·学习方法