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]
相关推荐
wearegogog1237 小时前
光谱分析波段选择的连续投影算法
算法
执笔论英雄7 小时前
【RL】DAPO 数据处理
算法
why1518 小时前
面经整理——算法
java·数据结构·算法
悦悦子a啊8 小时前
将学生管理系统改造为C/S模式 - 开发过程报告
java·开发语言·算法
痕忆丶9 小时前
双线性插值缩放算法详解
算法
_codemonster10 小时前
深度学习实战(基于pytroch)系列(四十八)AdaGrad优化算法
人工智能·深度学习·算法
鹿角片ljp10 小时前
力扣140.快慢指针法求解链表倒数第K个节点
算法·leetcode·链表
自由生长202410 小时前
位运算第1篇-异或运算-快速找出重复数字
算法
xxxxxmy11 小时前
同向双指针(滑动窗口)
python·算法·滑动窗口·同向双指针
释怀°Believe11 小时前
Daily算法刷题【面试经典150题-5️⃣图】
算法·面试·深度优先