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]
相关推荐
田梓燊3 分钟前
最长的连续序列到底怎么写
算法·哈希算法·散列表
smchaopiao7 分钟前
C++20概念(Concepts)入门指南
开发语言·c++·算法
一叶落43811 分钟前
LeetCode 21. 合并两个有序链表(C语言详解 | 链表经典题)
c语言·数据结构·c++·算法·leetcode·链表
阿里嘎多哈基米24 分钟前
速通Hot100-Day04——哈希
数据结构·算法·leetcode·哈希算法·散列表
飞天狗11140 分钟前
最短路算法
算法
汉克老师1 小时前
GESPC++考试五级语法知识(二、埃氏筛与线性筛)课后习题
算法·线性筛·素数·gesp5级·gesp五级·埃氏筛·筛法
xsyaaaan1 小时前
leetcode-hot100-普通数组:53最大子数组和-56合并区间-189轮转数组-238除了自身以外数组的乘积-41缺失的第一个正数
leetcode
0 0 01 小时前
洛谷P4427 [BJOI2018] 求和 【考点】:树上前缀和
开发语言·c++·算法·前缀和
佩奇大王1 小时前
P593 既约分数
java·开发语言·算法
云泽8081 小时前
蓝桥杯算法精讲:贪心算法之推公式例题深度剖析
算法·贪心算法·蓝桥杯