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]
相关推荐
秋天的怀念_David3 分钟前
算法一千零一夜 · 第四十夜 · 错层的状态码
java·程序人生·算法
纪念 2298 分钟前
C++ 算法模板与错题 (差分)
开发语言·c++·算法
echoliuy31 分钟前
二叉树—二叉搜索树 前序 中序 后序
算法·深度优先
怕浪猫35 分钟前
机器人是当下薪酬最高却几乎没人学的技能:一份 6 个月从 0 到上手的学习路线
算法·架构·github
MobotStone1 小时前
打造高性价比 AI 应用:选对模型、用好缓存、持续测试
人工智能·算法
金榜题名萱1 小时前
蓝桥杯|石子合并(区间动态规划)
c语言·数据结构·c++·算法·蓝桥杯·动态规划
纪念 2291 小时前
C++ 算法模板与错题 (前缀和)
开发语言·c++·算法
罗西的思考1 小时前
【轻量化复刻大模型 / π0.5】(4)处理输入
人工智能·算法·机器学习
饮水桶_T2 小时前
DUNE:基于依赖图传播的Agent派生记忆一致性更新机制 笔记
开发语言·笔记·算法·电脑
Navigator_Z2 小时前
LeetCode //C - 1332. Remove Palindromic Subsequences
c语言·算法·leetcode