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]
相关推荐
吕司14 小时前
LeetCode Hot Code——最大子数组和
数据结构·算法·leetcode
XiYang-DING14 小时前
【LeetCode】144. 二叉树的前序遍历
算法·leetcode·职场和发展
WolfGang00732114 小时前
代码随想录算法训练营 Day28 | 动态规划 part01
算法·动态规划
光电笑映14 小时前
STL 源码解密:unordered 系列容器的底层复用与哈希策略
算法·哈希算法·散列表
6Hzlia14 小时前
【Hot 100 刷题计划】 LeetCode 215. 数组中的第K个最大元素 | C++ 快速选择与堆排序题解
c++·算法·leetcode
小白菜又菜14 小时前
Leetcode 3070. Count Submatrices with Top-Left Element and Sum Less Than k
算法·leetcode·职场和发展
笨笨饿15 小时前
32_复变函数在工程中实际应用区别于联系
linux·服务器·c语言·人工智能·单片机·算法·学习方法
会编程的土豆15 小时前
【数据结构与算法】拓扑排序2
数据结构·算法·leetcode
Boop_wu15 小时前
[Java 算法] 栈
java·开发语言·算法
追风落叶乔木生15 小时前
字节跳动后端一面全解析|基础+算法真题(2026最新版)
算法·哈希算法