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]
相关推荐
学习2年半23 分钟前
小米笔试题:一元一次方程求解
算法
MATLAB代码顾问26 分钟前
MATLAB绘制多种混沌系统
人工智能·算法·matlab
极客BIM工作室1 小时前
演化搜索与群集智能:五种经典算法探秘
人工智能·算法·机器学习
qq_574656251 小时前
java-代码随想录第66天|Floyd 算法、A * 算法精讲 (A star算法)
java·算法·leetcode·图论
金融街小单纯2 小时前
从蓝军建设中学习颠覆性质疑思维
人工智能·算法·机器学习
少许极端3 小时前
算法奇妙屋(五)-链表
数据结构·算法·链表
XISHI_TIANLAN3 小时前
【多模态学习】Q&A6: 什么是MOE架构?Router Z Loss函数是指什么?负载均衡损失(Load Balancing Loss)又是什么?
学习·算法·语言模型
木子.李3473 小时前
数据结构-算法C++(额外问题汇总)
数据结构·c++·算法
花心蝴蝶.4 小时前
API签名认证算法全解析
算法
兮山与4 小时前
算法6.0
算法