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]
相关推荐
Evand J32 分钟前
【MATLAB例程】三维RRT+APF避障路径规划与到达角(AOA)定位算法|三维路径优化与定位仿真例程
算法·matlab·路径规划·代码·定位·rrt·aoa
weixin_307779132 小时前
有限产能智能排产与动态重排智能体:从需求解构到技术实现
开发语言·人工智能·算法·架构
朝朝辞暮i2 小时前
C++ 第 27 课:智能指针 shared_ptr
开发语言·c++·算法
朝朝辞暮i3 小时前
C++ 第 29 课:回调函数 Callback
开发语言·c++·算法
Godspeed Zhao3 小时前
现代智能雷达技术14——算法(0)
线性代数·算法·雷达
可乐ea3 小时前
Agent 模型分级升级路由:用置信度阈值把贵模型省下来
人工智能·算法·机器学习·结构化输出·置信度路由·模型升级路由·大模型成本优化
Navigator_Z3 小时前
LeetCode //C - 1278. Palindrome Partitioning III
c语言·算法·leetcode
Godspeed Zhao4 小时前
现代智能雷达技术18——算法(4)
算法
CHEEVEN_QY4 小时前
液冷板热阻计算与流阻优化的实战方法
人工智能·算法·机器学习
C++ 老炮儿的技术栈5 小时前
不一样的数值交换
数据结构·c++·人工智能·算法·c·csdn开发云