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]
相关推荐
碧海银沙音频科技研究院3 小时前
炬芯ATS3089X手表的HFP通话不自动路由到设备BT的问题解决
网络·人工智能·嵌入式硬件·算法
mmmmath_38 小时前
LeetCode.199.二叉树的右视图
数据结构·leetcode
专注仿真8 小时前
引擎扩展性与集成设计深度剖析:从 14 个扩展点到 DIS→HLA 演进之路
开发语言·算法·仿真引擎·推演引擎
码爸8 小时前
支持向量机介绍与示例
算法·机器学习·支持向量机
怕浪猫8 小时前
2028编程圈很火的10个Skills
算法·面试·github
Nil20813 小时前
leetcode 198打家劫舍
算法·leetcode·职场和发展
Shaoshing13 小时前
前缀和与差分
java·数据结构·算法·前缀和·差分
mmmmath_313 小时前
LeetCode.429.N叉树的层序遍历
数据结构·leetcode
loulanyue_14 小时前
构建房产认知决策智能:Harness底座与AI自进化的技术实践——读贝壳首席算法科学家尹俊希2026云栖专场演讲
人工智能·算法