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]
相关推荐
参.商.1 小时前
【Day 53】76. 最小覆盖子串
leetcode·golang
HZZD_HZZD1 小时前
CSDN_批发市场水电漏损归因算法LAM的原理与落地
嵌入式硬件·物联网·算法
shirsl3 小时前
算法 Day 5 树 / 二叉树 + DFS
数据结构·python·算法
木子算法3 小时前
非凸、离散、还耦合:论文里的求解方法是一条四步流水线
人工智能·算法·目标跟踪
虚无的纽扣3 小时前
【力扣刷题】第二天:无重复字符的最长字串、移动零问题
算法·leetcode·排序算法
张祥6422889044 小时前
牛顿迭代法求解开普勒方程:从RTKLIB源码到数值分析
人工智能·算法·机器学习
Niuguangshuo4 小时前
论文解读:Deep Speech 2,工业级英中端到端 ASR 系统报告
算法·音视频·语音识别
钓鱼的肝4 小时前
csp-j-s总结(2)
c++·经验分享·笔记·算法·青少年编程
奇妙之二进制4 小时前
机器人导航路径规划算法入门(6)Dijkstra(迪杰斯特拉)算法深入解析
算法·导航
Niuguangshuo5 小时前
论文解读:Qwen2-Audio,阿里的通用音频语言模型
算法·音视频·语音识别