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]
相关推荐
你也向往长安城吗19 分钟前
推荐一个三维导航库:three-pathfinding-3d
javascript·算法
百度智能云35 分钟前
VectorDB+FastGPT一站式构建:智能知识库与企业级对话系统实战
算法
AI小白的Python之路1 小时前
数据结构与算法-排序
数据结构·算法·排序算法
DashVector2 小时前
如何通过Java SDK检索Doc
后端·算法·架构
zzz9332 小时前
transformer实战——mask
算法
一只鱼^_2 小时前
牛客周赛 Round 105
数据结构·c++·算法·均值算法·逻辑回归·动态规划·启发式算法
是阿建吖!2 小时前
【动态规划】斐波那契数列模型
算法·动态规划
啊阿狸不会拉杆3 小时前
《算法导论》第 27 章 - 多线程算法
java·jvm·c++·算法·图论
火车叨位去19493 小时前
力扣top100(day04-05)--堆
算法·leetcode·职场和发展
数据智能老司机3 小时前
面向企业的图学习扩展——面向图的传统机器学习
算法·机器学习