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]
相关推荐
Beyond_System|系统之外33 分钟前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
漂流瓶jz2 小时前
UVA-1442 洞穴 题解答案代码 算法竞赛入门经典第二版
c++·算法·题解·aoapc·算法竞赛入门经典·uva
圣保罗的大教堂2 小时前
leetcode 835. 图像重叠 中等
leetcode
叠层归一研究院4 小时前
基于极限自指的叠层归一宇宙结构理论——无元外部封闭系统的内生区分模型
人工智能·经验分享·算法·agi
Selvaggia5 小时前
Diffusion Model 的基本原理与模型架构
算法
wabs6666 小时前
关于二叉树【力扣572.另一棵树的子树的思考】
数据结构·c++·算法·leetcode·二叉树
hanlin036 小时前
刷题笔记:力扣第41题-缺失的第一个正数
笔记·算法·leetcode
shehuiyuelaiyuehao7 小时前
算法50,分治归并,数组中的逆序对
java·算法
阳明山水7 小时前
校准分位数驱动智能库存决策
人工智能·深度学习·算法·机器学习·架构
别动我齐刘海8 小时前
ROS2 Jazzy + C++ 实战路线——进阶学习3
c++·人工智能·vscode·python·算法·机器学习·机器人