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]
相关推荐
沈阳信息学奥赛培训17 小时前
C++ 指针* 和 指针的引用 *& (不是指针和引用,是指针的引用)
数据结构·c++·算法
老鱼说AI17 小时前
《深入理解计算机系统》(CSAPP)2.2:整数数据类型与底层机器级表示
开发语言·汇编·算法·c#
会编程的土豆17 小时前
【数据结构与算法】 树
数据结构·算法
LSL666_18 小时前
Redis值数据类型——hash
redis·算法·哈希算法·数据类型
喵喵蒻葉睦18 小时前
力扣 hot100 滑动窗口最大值 单调双端队列 java 简单题解
java·数据结构·算法·leetcode·双端队列·滑动窗口·队列
样例过了就是过了18 小时前
LeetCode热题100 搜索二维矩阵
数据结构·c++·算法·leetcode·矩阵
2401_8319207418 小时前
C++与Qt图形开发
开发语言·c++·算法
Shining059618 小时前
AI 编译器系列(四)《AI 编译器中的后端优化》
linux·服务器·人工智能·线性代数·算法·triton·ai编译器
像污秽一样18 小时前
算法设计与分析-习题8.1
数据结构·算法·dfs·dp
飞天狗11118 小时前
最小生成树算法
算法