DP

LeetCode 518 Coin Change 2 (Python)

Posted by 小明MaxMing on June 7, 2020

题目

You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

解题思路

dp[i][j]表示用前i种硬币,构造出j的组合数,dp[len(coins)][amount]为所求

dp[0][0] = 1
dp[0][i] = 0    1 <= i <= amount
dp[i][j] = dp[i - 1][j] + dp[i][j - coins[i]]

代码

class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        dp = [1] + [0] * amount
        for c in coins:
            for i in range(c, amount + 1):
                dp[i] += dp[i - c]
        return dp[amount]

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅