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]
相关推荐
故事和你91几秒前
洛谷-算法2-2-常见优化技巧3
开发语言·数据结构·c++·算法·深度优先·动态规划·图论
菜鸟555555 分钟前
2025江西省CCPC省赛暨全国邀请赛(南昌)
数据结构·c++·算法·acm·思维·ccpc·xcpc
lds走自己的路18 分钟前
全局坐标转局部坐标推导
人工智能·算法·机器学习
6Hzlia22 分钟前
【Hot 100 刷题计划】 LeetCode 21. 合并两个有序链表 | C++ 经典迭代与 Dummy 技巧
c++·leetcode·链表
杨校24 分钟前
杨校老师课堂之C++高精度乘法
算法
上弦月-编程24 分钟前
C语言位运算:从入门到精通
运维·c语言·开发语言·vscode·算法·leetcode·极限编程
꧁细听勿语情꧂39 分钟前
用队列实现栈、用栈实现队列,树、二叉树、满二叉树、完全二叉树,堆、向下向上调整算法、出堆入堆、堆排序
c语言·开发语言·数据结构·算法
碧海银沙音频科技研究院41 分钟前
BES2800BP_nuttx编译环境搭建方法
人工智能·深度学习·算法
Felven1 小时前
B. Make Almost Equal With Mod
数据结构·算法
脆皮炸鸡7551 小时前
Linux~~基础IO
linux·运维·服务器·经验分享·算法·学习方法