LeetCode.518. Coin Change 2


地址:https://leetcode.com/problems/largest-palindrome-product

題意:

給一個總額amount, 給若干個面額不同的硬幣的集合coins, 問組成總額的方案數.

題解:

簡單DP.
dp[i]表示總額為i時的方案數.
轉移方程: dp[i] = Σdp[i - coins[j]]; 表示 總額為i時的方案數 = 總額為i-coins[j]的方案數的加和.
記得初始化dp[0] = 1; 表示總額為0時方案數為1.

代碼:

 1 class Solution(object):
 2     def change(self, amount, coins):
 3         """
 4         :type amount: int
 5         :type coins: List[int]
 6         :rtype: int
 7         """
 8         size = len(coins)
 9         dp = [1] + [0] * amount
10         for i in range(size):
11             for j in range(amount):
12                 if j + coins[i] <= amount:    
13                     dp[j + coins[i]] += dp[j]
14         return dp[-1]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM