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]
相关推荐
import_random2 分钟前
[社交网络]布局算法(可视化)
算法
地平线开发者33 分钟前
C++ 部署的性能优化方法
c++·算法·自动驾驶
怀念无所不能的你35 分钟前
acwing背包问题求方案数
学习·算法·动态规划·dp
Yingye Zhu(HPXXZYY)1 小时前
洛谷P12238 [蓝桥杯 2023 国 Java A] 单词分类
c++·算法·蓝桥杯
积极向上的向日葵2 小时前
链表的中间节点
数据结构·算法·链表·快慢指针
曾几何时`2 小时前
C++——哈希表
算法·哈希算法
Y1nhl2 小时前
力扣hot100_普通数组_python版本
开发语言·python·算法·leetcode·职场和发展
m0_504135304 小时前
代码随想录算法训练营第六十一天 | floyd算法
算法
xin007hoyo8 小时前
算法笔记.染色法判断二分图
数据结构·笔记·算法
এ᭄画画的北北11 小时前
力扣-234.回文链表
算法·leetcode·链表