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]
相关推荐
动词ing28 分钟前
【题目练习】全排列
算法
易小尊1 小时前
Dixon-Coles 的 τ 函数,我写错了三次
算法
凯瑟琳.奥古斯特2 小时前
力扣1015C++解法技巧
c++·算法·leetcode·链表·职场和发展
seowmt2 小时前
GEO效果该什么时候看数据:从立项到复盘的时间线复盘
算法·ip·seo·geo·faq
悠仁さん2 小时前
【算法C++】双指针(一)
数据结构·c++·算法
文丑颜不良啊2 小时前
LeetCode626~650题目解析
java·leetcode
小豆庄2 小时前
本地视频脱敏的追踪系统踩坑——车牌过滤、转场误判与ID冲突排查
人工智能·算法·目标检测·音视频·视频·敏捷流程·纹理贴图
忆挽篱笙歌2 小时前
进程替换函数与shenll命令行解释器
前端·javascript·算法
YukiRinLL2 小时前
字符串存储与 BF、KMP 匹配算法
数据结构·算法
小超人诞生日记2 小时前
Linux:环境变量 & 进程虚拟地址空间
linux·算法