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]
相关推荐
妄想出头的工业炼药师7 小时前
LVIO鲁棒
算法·开源
aini_lovee8 小时前
MATLAB 图像修复 — 偏微分方程方法
算法
Cthy_hy8 小时前
Python算法竞赛:排列组合核心用法
开发语言·python·算法
大圣编程8 小时前
面向对象深度理解
java·开发语言·算法
爱喝水的鱼丶8 小时前
SAP-ABAP:SAP 简单报表输出开发系列(共6篇) 第四篇:SAP 报表异常处理机制:数据校验与消息提示规范落地
开发语言·数据库·学习·算法·sap·abap
wabs6669 小时前
关于贪心算法【划分字母区间】的问题总结(C++语法)
算法·贪心算法
啦啦啦啦啦zzzz9 小时前
数据结构:二叉树的线索化
数据结构·算法
2401_8724187810 小时前
算法入门:并查集(Disjoint Set / Union-Find):连通性问题的利器
算法
luj_176810 小时前
R语言生态优势与学习曲线分析
c语言·开发语言·网络·经验分享·算法
计算机安禾10 小时前
【算法分析与设计】第36篇:计算几何基础:凸包问题的分治与扫描线解法
大数据·人工智能·算法·机器学习·剪枝