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]
相关推荐
人间乄惊鸿客15 小时前
c++ 右值
c++·算法
fanstuck16 小时前
AI 客服答错了,三分钟分清是 Prompt 还是系统的锅
算法
田里的水稻17 小时前
FA_编程算法200个分类列数-SUM
算法·分类·数据挖掘
猿人谷19 小时前
从百万个 Pareto 解中选出 100 个:Riesz s-Energy 如何实现高效的多样性选择?
后端·算法·aigc
地平线开发者19 小时前
地平线 × 同济无人车队:PointPillars 从浮点模型到 BPU 部署
算法·自动驾驶
iiiiii1120 小时前
从最大熵到 SAC:Soft Q-Learning 与 Soft Actor-Critic 公式推导笔记
人工智能·笔记·算法·机器学习·强化学习
田里的水稻21 小时前
FA_排序算法-冒泡排序
java·算法·排序算法
AAA修煤气灶刘哥1 天前
AI 写的 SQL 跑通了,为什么和财务报表对不上?
人工智能·后端·算法