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]
相关推荐
Frostnova丶18 分钟前
(13)LeetCode 53. 最大子数组和
算法·leetcode·职场和发展
什巳20 分钟前
JAVA练习277- 找到字符串中所有字母异位
java·开发语言·算法·leetcode
机器学习之心21 分钟前
基于遗传粒子群混合算法的无人机三维路径规划:Matlab 实现与多算法对比分析
算法·matlab·无人机·无人机三维路径规划·遗传粒子群混合算法
本王是暴君34 分钟前
AutoMem:把 Agent Memory 变成可训练的记忆管理技能
人工智能·算法·数据挖掘
zmzb010337 分钟前
C++课后习题训练记录Day154
数据结构·c++·算法
木木子226 小时前
# 密码强度检测深度解析:正则表达式实时分析、多维度评分算法与可视化反馈
mysql·算法·华为·正则表达式·harmonyos
Sw1zzle9 小时前
算法入门(四):二叉树 - 递归遍历三件套
算法·leetcode
万法若空9 小时前
【算法-查找】查找算法
java·数据结构·算法
海石9 小时前
子树怎么找?树的3种遍历方式来帮忙!
算法·leetcode
海石9 小时前
难度分 1588:思路 + 技巧 = AC
算法·leetcode