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]
相关推荐
bIo7lyA8v2 分钟前
算法中的随机化思想及其复杂度收益评估的技术8
算法
数据法师7 分钟前
视频文件重复检测工具:基于哈希与视频指纹的三级筛选机制
算法·音视频·哈希算法
其实防守也摸鱼9 分钟前
软件安全与漏洞--Windows底层原理与软件逆向工程基础
linux·网络·数据库·算法·安全·安全架构·软件安全与漏洞
bIo7lyA8v39 分钟前
算法稳定性与数据分布的内在联系研究的技术8
算法
bIo7lyA8v1 小时前
算法可视化对教学与调试效率的影响分析的技术8
算法
hunterkkk(c++)2 小时前
优先队列启发式最短路径快速算法(优化SPFA)-HEAP_SPFA算法
算法
SiliconGazer2 小时前
第15届国赛满分代码解析(下)—— 运动轨迹算法、按键交互与完整状态机
算法·状态机·stc15f2k60s2·浮点运算·蓝桥杯国赛·运动轨迹、·向量分解
Navigator_Z2 小时前
LeetCode //C - 1096. Brace Expansion II
c语言·算法·leetcode
luj_17682 小时前
FreeDOS vs MS-DOS PC-DOS 对比解析
服务器·c语言·开发语言·经验分享·算法
笨笨没好名字2 小时前
Leetcode刷题python版第一周
python·算法·leetcode