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]
相关推荐
code_pgf19 分钟前
改进模型架构来减少MLLMs中的幻觉现象
人工智能·深度学习·算法
2301_7644413326 分钟前
基于AI的本地文件归档智能管理工具梳理
人工智能·python·算法·目标检测·交互
无限码力33 分钟前
美团研发岗 4月18号笔试真题 - 包包的最长公共子序列3
算法·美团笔试题·美团研发岗笔试题·美团机试题
怪兽学LLM1 小时前
LeetCode 21 合并两个有序链表:彻底理解虚拟头节点(Dummy)套路
python·leetcode·链表
阿里matlab建模师1 小时前
基于matlab时域频域处理的语音信号变声处理系统设计与算法原理(论文+程序源码+GUI图形用户界面)——变声算法
算法·matlab·语音识别
IMPYLH1 小时前
HTML 的 <abbr> 元素
前端·算法·html
leo__5201 小时前
小波特征与模糊支持向量机(FSVM)的脑电信号分类方法
算法·支持向量机·分类
wabs6661 小时前
关于动态规划【纯粹的0-1背包需要思考的问题】
算法·动态规划
小小编程路1 小时前
字符串转数字时,可能会遇到哪些问题?
java·开发语言·算法
rit84324991 小时前
MATLAB近红外光谱预处理:平滑与求导(MSV方法)
数据结构·算法·matlab