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]
相关推荐
纪念 22929 分钟前
C++算法(三)
开发语言·c++·算法
学不会pwn不改名43 分钟前
时间复杂度与空间复杂度详解
算法
AbandonForce1 小时前
递归、搜索与回溯:从板书到二叉树解题方法
c++·算法·决策树
打工仔折腾 AI1 小时前
让时序预测真正参与决策:TimechoAI 工程化落地与管线设计实践
人工智能·后端·python·算法·机器学习·性能优化
Light601 小时前
企业自建 AI 智能体,成本差 8 倍还是 46 倍?
java·人工智能·算法·知识图谱·rag·ai agent 上下文·token 成本
曹牧1 小时前
VS: Visual Studio 里把未包含的文件加进项目2
linux·运维·算法
重生之小比特2 小时前
【算法拾光】滑动窗口|长度最小的子数组 + 无重复字符的最长子串
算法
weixin_307779132 小时前
C++代码实现MATLAB中的fitnlm函数功能
开发语言·c++·算法·matlab
雪落漂泊2 小时前
C++算法:双指针篇(下)
数据结构·c++·算法