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]
相关推荐
Kk.080219 分钟前
Linux(十一)fork实例练习、文件操作示例及相关面试题目分享
linux·运维·算法
潇冉沐晴1 小时前
2026CCCC第三次模拟赛 部分题解
算法
WolfGang0073211 小时前
代码随想录算法训练营 Day32 | 动态规划 part05
算法·动态规划
碧海银沙音频科技研究院2 小时前
1-1杰理蓝牙SOC的UI配置开发方法
人工智能·深度学习·算法
啊我不会诶2 小时前
2024CCPC长春邀请赛
算法
珂朵莉MM2 小时前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--启发式算法+操作因子设计
人工智能·算法
CS创新实验室3 小时前
CS实验室行业报告:AI算法工程师就业分析报告
人工智能·算法
XiYang-DING4 小时前
【LeetCode】Hash | 136.只出现一次的数字
算法·leetcode·哈希算法
wayz114 小时前
Day 3:逻辑回归与分类预测
算法·分类·逻辑回归