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]
相关推荐
Navigator_Z44 分钟前
LeetCode //C - 1156. Swap For Longest Repeated Character Substring
c语言·算法·leetcode
Reart1 小时前
Leetcode 1143.最长公共子序列(720)
后端·算法
无相求码1 小时前
const vs #define:C语言常量定义的差异
c语言·算法
先吃饱再说1 小时前
LeetCode 226. 翻转二叉树
算法
剑锋所指,所向披靡!2 小时前
数据结构之关键路径
数据结构·算法
阿宇的技术日志2 小时前
漏桶、令牌桶、滑动窗口 三限流算法理解
算法·滑动窗口·漏桶·令牌桶
来一碗刘肉面2 小时前
队列的链式实现
数据结构·c++·算法·链表
KaMeidebaby2 小时前
卡梅德生物技术快报|原核膜蛋白表达优化实操手册,膜蛋白的纯化梯度洗脱完整流程
前端·网络·数据库·人工智能·算法
naturerun2 小时前
逐步插入回路法构造欧拉回路的算法
c++·算法
qizayaoshuap2 小时前
# [特殊字符] 骰子模拟器 — 鸿蒙ArkTS随机算法与动画系统设计
算法·华为·harmonyos