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]
相关推荐
民乐团扒谱机12 小时前
【读论文】引力与惯性的起源:从全息原理到牛顿定律与爱因斯坦方程
算法·量子力学··万有引力·爱因斯坦方程·全息原理·牛顿定律
努力学算法的蒟蒻12 小时前
day84(2.13)——leetcode面试经典150
算法·leetcode·面试
@––––––12 小时前
力扣hot100—系列8-回溯算法
javascript·算法·leetcode
!停12 小时前
数据结构二叉树—堆(2)&链式结构(上)
数据结构·算法
C++ 老炮儿的技术栈12 小时前
万物皆文件:Linux 抽象哲学的开发之美
c语言·开发语言·c++·qt·算法
im_AMBER12 小时前
Leetcode 120 求根节点到叶节点数字之和 | 完全二叉树的节点个数
数据结构·学习·算法·leetcode·二叉树·深度优先
1027lonikitave12 小时前
FFTW的expr.ml怎么起作用
算法·哈希算法
TracyCoder12312 小时前
LeetCode Hot100(54/100)——215. 数组中的第K个最大元素
算法·leetcode·排序算法
We་ct12 小时前
LeetCode 92. 反转链表II :题解与思路解析
前端·算法·leetcode·链表·typescript
春日见12 小时前
如何查看我一共commit了多少个,是哪几个,如何回退到某一个版本
vscode·算法·docker·容器·自动驾驶