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]
相关推荐
Zero2 小时前
机器学习微积分--(1)核心思想
人工智能·算法·机器学习
有Li2 小时前
一种病理学内容感知变速率学习图像压缩框架 (PathoLIC)/文献速递-多模态应用技术
人工智能·深度学习·算法·计算机视觉·医学生
x_xbx2 小时前
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
数据结构·算法·leetcode
Ricky_Theseus2 小时前
数据库关系代数 - 连接操作
linux·数据库·算法
绿算技术3 小时前
宝辰股份董事长莅临绿算技术调研交流
人工智能·科技·算法
码云数智-园园3 小时前
哈希冲突的解决之道:深入理解哈希表底层原理
算法·哈希算法
qq_416018723 小时前
C++中的模板方法模式
开发语言·c++·算法
天上路人3 小时前
A-59F 多功能语音处理模组在本地会议系统扩音啸叫处理中的技术应用与性能分析
人工智能·神经网络·算法·硬件架构·音视频·语音识别·实时音视频
yang_B6214 小时前
噪声处理方法
大数据·人工智能·算法
菜菜小狗的学习笔记4 小时前
剑指Offer算法题(九)搜索
数据结构·算法·深度优先