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]
相关推荐
故事和你916 分钟前
洛谷-算法2-3-分治与倍增5
开发语言·数据结构·c++·算法·动态规划·图论
北顾笙98023 分钟前
day37-数据结构力扣
数据结构·算法·leetcode
啦啦啦_999928 分钟前
1. 逻辑回归
算法·机器学习·逻辑回归
fengenrong1 小时前
20260429
c++·算法
南宫萧幕1 小时前
Python与Simulink联合仿真:基于DQN的HEV能量管理策略建模与全链路排雷实战
开发语言·人工智能·python·算法·机器学习·matlab·控制
apollowing1 小时前
启发式算法WebApp实验室:从搜索策略到群体智能的能力进阶(优)
算法·启发式算法·web app
Chat_zhanggong3452 小时前
主推NT98336BG作用有哪些?
嵌入式硬件·算法
Run_Teenage3 小时前
算法:线段树
算法
Westward-sun.3 小时前
YOLOv2算法全方位解析:从BatchNorm到聚类先验框的九大改进
算法·yolo·聚类
扶苏xw3 小时前
【离散化算法】
算法