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]
相关推荐
cc.ChenLy1 小时前
算法从入门到精通实战指南
算法
Jerry8 小时前
LeetCode 160. 相交链表
算法
Jerry8 小时前
LeetCode 19. 删除链表的倒数第 N 个结点
算法
金銀銅鐵8 小时前
费马小定理
python·数学·算法
技术不好的崎鸣同学10 小时前
[ACTF2020 新生赛]Exec 思路及解法
算法·安全·web安全
Full Stack Developme11 小时前
Java LRU 与 LFU 算法及应用
java·开发语言·算法
Jerry12 小时前
LeetCode 707. 设计链表
算法
C语言小火车13 小时前
C++ 堆排序深度精讲:基于完全二叉树的选择排序进化,最坏情况 O(n log n) 的稳定王者
开发语言·c++·算法·排序算法·堆排序
weixin_4000056013 小时前
Vision-Language-Action:LMDrive双损失函数训练模块与 LangAuto 基准评测框架
人工智能·深度学习·算法·机器学习·自动驾驶