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]
相关推荐
大大杰哥6 分钟前
leetcode hot100(1) 哈希
leetcode
爱写代码的倒霉蛋11 分钟前
2021天梯赛L2-4真题解析
数据结构·算法
hoiii18712 分钟前
基于CVX的储能调峰调频优化模型
算法
啦啦啦_999915 分钟前
KNN算法
算法
Engineer邓祥浩21 分钟前
LeetCode 热题 100 - 第1题:两数之和
算法·leetcode·职场和发展
white-persist21 分钟前
逆向入门经典题:从 IDA 反编译坑点到 Python 解题详细分析解释
c语言·开发语言·数据结构·python·算法·逆向·安全架构
炽烈小老头22 分钟前
【每天学习一点算法 2026/04/23】盛最多水的容器
学习·算法
Ailan_Anjuxi27 分钟前
手写数字识别零基础实战:基于PyTorch的CNN完整拆解
算法·图像识别
jiucaixiuyang29 分钟前
散户如何使用手机T0算法?
算法·量化·t0
阿Y加油吧41 分钟前
算法二刷复盘:LeetCode 79 单词搜索 & 131 分割回文串(Java 回溯精讲)
java·算法·leetcode