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]
相关推荐
汀、人工智能1 分钟前
[特殊字符] 第78课:乘积最大子数组
数据结构·算法·数据库架构·数组·前缀积·乘积最大子数组
tankeven1 分钟前
HJ168 小红的字符串
c++·算法
数据知道4 分钟前
claw-code 源码分析:cargo 视角的 definitive runtime——会话、压缩、MCP、提示构造如何落到系统语言?
算法·ai·claude code·claw code
汀、人工智能7 分钟前
[特殊字符] 第41课:翻转二叉树
数据结构·算法·数据库架构·图论·bfs·翻转二叉树
2301_822703208 分钟前
大学生体质健康测试全景测绘台:基于鸿蒙Flutter的多维数据可视化与状态管理响应架构
算法·flutter·信息可视化·架构·开源·harmonyos·鸿蒙
鲸渔8 分钟前
【C++ 输入输出】cin、cout、cerr 与格式化输出
开发语言·c++·算法
汀、人工智能8 分钟前
[特殊字符] 第46课:验证二叉搜索树
数据结构·算法·数据库架构·图论·bfs·验证二叉搜索树
靠沿13 分钟前
【递归、搜索与回溯算法】专题三——穷举vs暴搜vs深搜vs回溯vs剪枝
算法·机器学习·剪枝
香蕉鼠片13 分钟前
排序算法C++
c++·算法·排序算法
xiaoye-duck16 分钟前
《算法题讲解指南:优选算法-栈》--65.删除字符中的所有相邻重复项,66.比较含退格的字符串,67.基本计算器II,68.字符串解码,69.验证栈序列
c++·算法·