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]
相关推荐
篮l球场12 分钟前
合并 K 个升序链表
算法
苦藤新鸡12 分钟前
87.分割成两个等和数组 leetcode416
数据结构·算法·leetcode
炽烈小老头15 分钟前
【 每天学习一点算法 2026/03/11】从前序与中序遍历序列构造二叉树
学习·算法
进击切图仔15 分钟前
ROS 行为(Action)机制
算法
飞Link17 分钟前
概率图模型的基石:隐马可夫模型 (HMM) 深度解析
开发语言·python·算法
_日拱一卒17 分钟前
LeetCode(力扣):验证回文串
算法·leetcode·职场和发展
Eward-an19 分钟前
LeetCode 128. 最长连续序列(O(n)时间复杂度详解)
数据结构·算法·leetcode
Frostnova丶20 分钟前
LeetCode 1009 & 476 数字的补数
算法·leetcode
CppBlock21 分钟前
HPX vs TBB vs OpenMP:并行任务模型对比
c++·算法