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]
相关推荐
穿条秋裤到处跑11 分钟前
每日一道leetcode(2026.04.19):下标对中的最大距离
算法·leetcode·职场和发展
Sag_ever23 分钟前
时间复杂度与空间复杂度超详细入门讲解
算法
念越25 分钟前
算法每日一题 Day03|快慢双指针解决快乐树问题
算法·力扣
ZPC821027 分钟前
MoveGroup 规划轨迹 → 直接交给 MoveIt2 Servo 执行
人工智能·算法·计算机视觉·机器人
️是7837 分钟前
信息奥赛一本通—编程启蒙(3373:练64.2 图像旋转翻转变换)
数据结构·c++·算法
木子墨5161 小时前
LeetCode 热题 100 精讲 | 计算几何篇:点积叉积 · 线段相交 · 凸包 · 多边形面积
c++·算法·leetcode·职场和发展·动态规划
源码之家1 小时前
计算机毕业设计:Python棉花产业数据可视化与预测系统 Django框架 ARIMA算法 数据分析 可视化 爬虫 大数据 大模型(建议收藏)✅
人工智能·python·算法·信息可视化·数据挖掘·django·课程设计
py有趣1 小时前
力扣热门100题之最小路径和
算法·leetcode
qeen871 小时前
【算法笔记】前缀和经典题目解析
c语言·c++·笔记·学习·算法
Je1lyfish1 小时前
Haskell 初探
开发语言·笔记·算法·rust·lisp·抽象代数