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]
相关推荐
踩坑记录22 分钟前
递归回溯本质
leetcode
zmzb010331 分钟前
C++课后习题训练记录Day105
开发语言·c++·算法
好学且牛逼的马1 小时前
【Hot100|25-LeetCode 142. 环形链表 II - 完整解法详解】
算法·leetcode·链表
H Corey1 小时前
数据结构与算法:高效编程的核心
java·开发语言·数据结构·算法
SmartBrain2 小时前
Python 特性(第一部分):知识点讲解(含示例)
开发语言·人工智能·python·算法
01二进制代码漫游日记2 小时前
自定义类型:联合和枚举(一)
c语言·开发语言·学习·算法
小学卷王2 小时前
复试day25
算法
样例过了就是过了3 小时前
LeetCode热题100 和为 K 的子数组
数据结构·算法·leetcode
二年级程序员3 小时前
单链表算法思路详解(下)
c语言·数据结构·算法
HAPPY酷3 小时前
C++ 成员指针(Pointer to Member)完全指南
java·c++·算法