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]
相关推荐
sali-tec30 分钟前
C# 基于halcon的视觉工作流-章56-彩图转云图
人工智能·算法·计算机视觉·c#
黑岚樱梦4 小时前
代码随想录打卡day23:435.无重叠区间
算法
Kuo-Teng4 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode
gihigo19985 小时前
MATLAB使用遗传算法解决车间资源分配动态调度问题
算法·matlab
墨染点香5 小时前
LeetCode 刷题【138. 随机链表的复制】
算法·leetcode·链表
却道天凉_好个秋6 小时前
目标检测算法与原理(一):迁移学习
算法·目标检测·迁移学习
兮山与7 小时前
算法24.0
算法
晓北斗NorSnow7 小时前
机器学习核心算法与学习资源解析
学习·算法·机器学习
hans汉斯8 小时前
【计算机科学与应用】基于BERT与DeepSeek大模型的智能舆论监控系统设计
大数据·人工智能·深度学习·算法·自然语言处理·bert·去噪