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]
相关推荐
QiLinkOS5 小时前
极客精神与商业思维的融合实践(3)
c语言·c++·人工智能·算法·开源协议
bIo7lyA8v5 小时前
算法设计中的代价函数优化与约束求解的技术8
算法
暖阳华笺5 小时前
【数据结构与算法】哈希专题
数据结构·c++·算法·leetcode·哈希算法
ceclar1235 小时前
C#字节流与字符流
算法·c#·.net
大白话_NOI5 小时前
【洛谷 P1024 】[NOIP2001 提高组] 一元三次方程求解 - 详细分析与C++实现
c++·算法
Matthew_zhu_5 小时前
P3374 【模板】树状数组 1 题解
算法
随意起个昵称5 小时前
区间dp-进阶题目1(进阶合并)
c++·算法·动态规划
伶俜665 小时前
鸿蒙原生应用实战(四)ArkUI 语音变声器:录音 + 4 种音效 + 音调变换算法
算法·华为·harmonyos
AKA__Zas5 小时前
芝士算法(滑动窗口片 2.0)
java·算法·leetcode·学习方法
变量未定义~5 小时前
摆放小球 、dp求解组合数、求解组合数2
数据结构·算法