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]
相关推荐
什巳10 分钟前
JAVA练习312- 二叉搜索树中第 K 小的元素
java·数据结构·算法·leetcode
froyoisle20 分钟前
CSP 真题解析:[CSP-J 2020-T3] 表达式
c++·算法·csp·信息学·信奥赛
geats人山人海23 分钟前
算法好题 2026.7.18
算法
文祐32 分钟前
C++类之虚函数表没有虚继承的菱形继承
开发语言·c++·算法
Lumos18641 分钟前
51单片机从零到实战(8)——串口通信
算法
小帽子_1231 小时前
大功率 PCS 双环闭环控制算法原理与实现
算法
buhuizhiyuci2 小时前
【算法篇】位运算 —— 基础篇
java·数据库·算法
Black蜡笔小新2 小时前
算法训练完了怎么上线?DLTM→AIS→EasyGBS三步交付流水线
人工智能·算法
sycmancia3 小时前
Qt——多线程与界面组件的通信
开发语言·qt·算法
想吃火锅10053 小时前
【leetcode】5.最长回文子串js
算法·leetcode·职场和发展