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]
相关推荐
叠层归一研究院4 小时前
如何用程序搭建一个 AGI 种子系统(三):生长如何对接物理与数学宇宙
人工智能·python·算法·机器学习·transformer·agi
show4334 小时前
2026小程序端AI智能优化技术实践:转文字后自动纠错+润色+分段算法分析
人工智能·算法·小程序
平生不晚5 小时前
在 SVG 体系里画一条任意的线
前端·算法
evans在进步5 小时前
LeetCode 198:打家劫舍——Java 动态规划详解
java·leetcode·动态规划
逆境不可逃6 小时前
LeetCode 双题:415. 字符串相加与 143. 重排链表
算法
2601_950760796 小时前
TNF-α:自身免疫疾病治疗的核心靶点与信号通路解析
人工智能·算法·蛋白
不会代码的小猴6 小时前
2. 了解Qt
开发语言·c++·笔记·qt·算法
ZC跨境爬虫6 小时前
LeetCode 119. 杨辉三角 II(原地更新优化详解 + Java Python 实现)
java·python·leetcode
比奇堡裤头村6 小时前
统计学习方法——支持向量机
算法·支持向量机·学习方法
吃着火锅x唱着歌6 小时前
LeetCode 3597.分割字符串
算法·leetcode·职场和发展