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]
相关推荐
文丑颜不良啊8 小时前
LeetCode1941~1960题目解析
java·leetcode
老师我太想进步了202612 小时前
【数据结构-4】算法美学之顺序表
数据结构·算法
风影同志7832412 小时前
编码器是激活函数吗?—— 一个看起来很像、其实不是的类比
算法
UIU11412 小时前
P1481 魔族密码
c++·算法·动态规划
文丑颜不良啊13 小时前
LeetCode1961~1980题目解析
java·leetcode
文丑颜不良啊13 小时前
LeetCode2001~2020题目解析
java·leetcode
加宝的起点雷达15 小时前
40 条候选里 38 条来自同一个源,主卡整屏重样
人工智能·算法·创业
秋天的怀念_David15 小时前
算法一千零一夜 · 第二十六夜 协议的母语
java·spring boot·算法
文丑颜不良啊16 小时前
LeetCode2021~2040题目解析
java·leetcode