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]
相关推荐
代码栈上的思考1 小时前
双指针法:从三道经典题看双指针的核心思想
数据结构·算法
J-TS1 小时前
线性自抗扰控制LADRC
c语言·人工智能·stm32·单片机·算法
Ivanqhz1 小时前
半格与数据流分析的五个要素(D、V、F、I、Λ)
开发语言·c++·后端·算法·rust
董厂长2 小时前
用 LangGraph 实现 Small-to-Big 分块检索策略
人工智能·算法·rag
大江东去浪淘尽千古风流人物2 小时前
【Sensor】IMU传感器选型车轨级 VS 消费级
人工智能·python·算法·机器学习·机器人
坚持编程的菜鸟2 小时前
互质数的个数
c语言·算法
ICscholar2 小时前
具身智能‘Affordance‘理解
人工智能·学习·算法
wangwangmoon_light2 小时前
1.2 LeetCode总结(线性表)_双指针
算法·leetcode·职场和发展
琢磨先生David2 小时前
Java算法每日一题
java·开发语言·算法
重生之后端学习2 小时前
114. 二叉树展开为链表
java·数据结构·算法·链表·职场和发展·深度优先