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]
相关推荐
普通攻击往后拉5 小时前
Leetcode 206. 反转链表
算法·leetcode·链表
@syh.5 小时前
【贪心】矩阵消除游戏
算法·游戏·矩阵
可编程芯片开发6 小时前
基于零极点配置的PID控制系统simulink建模与仿真
算法
徐小夕6 小时前
开源!我用SQLite + DuckDB打造了一款可视化AI问数平台
前端·算法·github
Hrain-AI6 小时前
2026 企业 AI 智能体平台横评:8 大主流平台 7 维度实测对比
人工智能·算法·机器学习
Angel Q.7 小时前
因子分析和生成模型有什么关系?从“幕后因素”到“生成数据”
算法
FBI HackerHarry浩9 小时前
AI大模型开发V2第四阶段线性回归
人工智能·算法·线性回归
Jerry10 小时前
LeetCode 108. 将有序数组转换为二叉搜索树
算法
蓝斯49711 小时前
Diff算法的简单介绍
算法