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]
相关推荐
MimCyan1 分钟前
LeetCode hot 100 (8-11,自用2026.04.03)
leetcode
Book思议-2 分钟前
【数据结构】二叉树小题
数据结构·算法
CoderCodingNo5 分钟前
【GESP】C++五级练习题 luogu-P1303 A*B Problem | 高精度计算
数据结构·c++·算法
故事和你915 分钟前
洛谷-算法1-1-模拟与高精度2
开发语言·数据结构·c++·算法·动态规划
B1acktion5 分钟前
2.6.堆排序——从堆结构到 Top-K,一套思路贯穿排序与选择
数据结构·c++·算法·排序算法
17(无规则自律)8 分钟前
【华为机考真题】魔法相册的重复记忆 C++ 实现
c++·算法·华为
木斯佳8 分钟前
前端八股文面经大全:腾讯PCG暑期前端一面(2026-04-01)·面经深度解析
前端·算法·面经·计算机原理
Q741_14710 分钟前
每日一题 力扣 3661. 可以被机器人摧毁的最大墙壁数目 双指针 动态规划 C++ 题解
c++·算法·leetcode·机器人·动态规划
alphaTao15 分钟前
LeetCode 每日一题 2026/3/30-2026/4/5
算法·leetcode·职场和发展
圣保罗的大教堂9 小时前
leetcode 3418. 机器人可以获得的最大金币数 中等
leetcode