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]
相关推荐
AI科技星3 分钟前
张祥前统一场论 22 个核心公式及常数
服务器·人工智能·线性代数·算法·矩阵·概率论
苏婳6664 分钟前
阿里巴巴校招软件笔试题经典
算法
阿猿收手吧!10 分钟前
【数据结构】高效掌握并查集:核心原理与实战
数据结构·算法
励ℳ13 分钟前
机器学习之线性回归算法:从原理到实践的全面解析
算法·机器学习·线性回归
_Twink1e14 分钟前
[算法教学]一、前置知识
算法
MicroTech202521 分钟前
微算法科技(NASDAQ: MLGO)使用量子傅里叶变换(QFT),增强图像压缩和滤波效率
科技·算法·量子计算
㓗冽25 分钟前
矩阵问题(二维数组)-基础题70th + 发牌(二维数组)-基础题71th + 数字金字塔(二维数组)-基础题72th
c++·算法·矩阵
芜湖xin35 分钟前
【题解-Acwing】796. 子矩阵的和
算法·前缀和
shehuiyuelaiyuehao36 分钟前
23七大排序算法
数据结构·算法·排序算法
Σίσυφος190038 分钟前
E=[T]×R 的证明
算法