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 小时前
profiler debug 工具用法与高一致性策略
算法·自动驾驶
编程大师哥4 小时前
匿名函数 lambda + 高阶函数
java·python·算法
我叫袁小陌4 小时前
算法解题思路指南
算法
地平线开发者4 小时前
Conv+BN+Add+ReLU 融合机制简介
算法·自动驾驶
yuanyuan2o24 小时前
模型预训练:Hugging Face Transformers 基础
算法·ai·语言模型·自然语言处理·nlp·深度优先
杨充4 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法
妄想出头的工业炼药师5 小时前
GS slam mono
算法·开源
_日拱一卒6 小时前
LeetCode:207课程表
java·数据结构·算法·leetcode·职场和发展
用户987409238878 小时前
llamafactory 0.6.3 没有 llamafactory-cli
算法