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]
相关推荐
房开民6 小时前
可变参数模板
java·开发语言·算法
_深海凉_6 小时前
LeetCode热题100-最小栈
java·数据结构·leetcode
不知名的忻6 小时前
Morris遍历(力扣第99题)
java·算法·leetcode·morris遍历
状元岐6 小时前
C#反射从入门到精通
java·javascript·算法
_深海凉_7 小时前
LeetCode热题100-除了自身以外数组的乘积
数据结构·算法·leetcode
Kk.08028 小时前
项目《基于Linux下的mybash命令解释器》(一)
前端·javascript·算法
SteveSenna8 小时前
Trossen Arm MuJoCo自定义1:改变目标物体
人工智能·学习·算法·机器人
yong99908 小时前
IHAOAVOA:天鹰优化算法与非洲秃鹫优化算法的混合算法(Matlab实现)
开发语言·算法·matlab
米粒19 小时前
力扣算法刷题 Day 42(股票问题总结)
算法·leetcode·职场和发展
浅念-11 小时前
从LeetCode入门位运算:常见技巧与实战题目全解析
数据结构·数据库·c++·笔记·算法·leetcode·牛客