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]
相关推荐
2401_8331977312 分钟前
C++代码切片分析
开发语言·c++·算法
月落归舟12 分钟前
每日算法题 14---14.环形链表
数据结构·算法·链表
m0_6214385212 分钟前
实时音频处理C++实现
开发语言·c++·算法
weixin_4219226915 分钟前
模板代码性能测试
开发语言·c++·算法
Liu6288827 分钟前
C++中的模板方法模式
开发语言·c++·算法
qq_3349031534 分钟前
高性能网络协议栈
开发语言·c++·算法
光电笑映36 分钟前
STL 源码解剖系列:map/set 的底层复用与红黑树封装
c语言·数据结构·c++·算法
阿贵---38 分钟前
模板编译期循环展开
开发语言·c++·算法
2601_9540236638 分钟前
Beyond the Hype: Deconstructing the 2025 High-Performance Stack for Agencies
java·开发语言·算法·seo·wordpress·gpl
沉鱼.4438 分钟前
滑动窗口问题
数据结构·算法