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]
相关推荐
NEFU_Jyj9 小时前
牛客周赛R168
算法
文丑颜不良啊16 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z17 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH17 小时前
C++ 标准库算法全景指南
数据结构·c++·算法
冻柠檬飞冰走茶18 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊18 小时前
LeetCode2161~2180题目解析
java·leetcode
雪落漂泊18 小时前
C++算法 二分查找篇(上)
开发语言·c++·算法
文丑颜不良啊20 小时前
LeetCode2141~2160题目解析
java·leetcode
LabVIEW开发20 小时前
LabVIEW 连接板:28 个端子怎么排,优先级怎么定
java·算法·labview