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]
相关推荐
样例过了就是过了23 分钟前
LeetCode热题100 路径总和 III
数据结构·c++·算法·leetcode·链表
lxh011324 分钟前
函数防抖题解
前端·javascript·算法
再难也得平29 分钟前
力扣41. 缺失的第一个正数(Java解法)
数据结构·算法·leetcode
颜酱29 分钟前
环检测与拓扑排序:BFS/DFS双实现
javascript·后端·算法
IronMurphy36 分钟前
【算法二十】 114. 寻找两个正序数组的中位数 153. 寻找旋转排序数组中的最小值
java·算法·leetcode
实心儿儿37 分钟前
算法2:链表的中间结点
数据结构·算法·链表
代码探秘者38 分钟前
【Java集合】ArrayList :底层原理、数组互转与扩容计算
java·开发语言·jvm·数据库·后端·python·算法
颜酱40 分钟前
理解并查集Union-Find:从原理到练习
javascript·后端·算法
玛卡巴卡ldf42 分钟前
【LeetCode 手撕算法】(双指针) 1-两数之和、283-移动零、11-盛最多水的容器、15-三数之和
数据结构·算法·leetcode
mygugu1 小时前
归纳理解epoch、batch、batch size、step、iteration深度学习名词
人工智能·算法