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]
相关推荐
古城小栈5 小时前
为啥说:训练用BF16,推理用FP16
人工智能·算法·机器学习
KaMeidebaby5 小时前
卡梅德生物技术快报|蛋白 N 端测序在重组贻贝融合蛋白表征中的应用,解决原核表达序列偏移工艺难题
前端·人工智能·物联网·算法·百度
Turbo正则6 小时前
群论在AI中的应用概述
人工智能·算法·抽象代数
ysa0510306 小时前
【并查集】判环
c++·笔记·算法
Jerry7 小时前
KeetCode 44. 开发商购买土地
算法
Jerry7 小时前
KeetCode 58. 区间和
算法
Jerry8 小时前
LeetCode 209. 长度最小的子数组
算法
彦为君8 小时前
算法思维与经典智力题
java·前端·redis·算法
智能优化与强化学习8 小时前
Gym(Gymnasium)仿真环境详解(二):环境简介、入门算法、调参要点、核心挑战
算法·强化学习·gym·零基础入门·算法评估
mxwin9 小时前
Unity Shader exp 函数的算法与渲染应用
算法·unity·游戏引擎·shader