https://leetcode.com/problems/coin-change/
coins = [1,2,5] 代表 面额为1,2,5的硬币,
求组合的加总为amount的最小所需硬币数。
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [amount+1]*(amount+1)
dp[0] = 0
# max = amount(all consist of 1)+1
'''
ex: amount = 11 means 11's 1 if have coin 1
To set max surely, we can plus 1.
[12,...,12]
?: number of combination
dp[0] = 0
dp[1] = ?
dp[2] = ??
...
dp[amount] = ??...
Run by actual value
[1,2,5]
dp[1] = 1 # 1x(1)=1
dp[2] = dp[1]+1 or dp[2-2]+1 -- select minimum
1) 1+1 (2)
2) 2 (1) ... v
How to process no solution(return -1) ?
Ans: compare default value of dp and dp[amount]
'''
for i in range(1,amount+1):
#print(i)
for coin in coins:
if i-coin>=0:
#print("In.")
dp[i] = min(dp[i-coin]+1,dp[i])
return dp[amount] if dp[amount]!=(amount+1) else -1
DP+DFS(Brute Force solution)
需考虑每个组合,Greedy法 not work !
YT-Coin Change - Dynamic Programming Bottom Up - Leetcode 322
Medium
>>: Day 18: 人工智慧在音乐领域的应用 (AI作曲-基因演算法二)
还记得在中坜上课时,吴老师常说:Java因介面而伟大。 初学物件导向程序概念,还真的不太能体悟为什麽...
什麽是挖矿?需要准备铁镐吗? 说到挖矿,可能很多人会想到的是显卡的涨价,让大家都觉得挖矿就是用显卡来...
做完Player後先帮来上个简易武器吧,找到Player Bullet 0一样拖曳到场景,看是否需要...
接续Day_10 有线网路应用(三),整理遇到的问题与补充说明。 Troubleshooting 网...
今天要来介绍这个主题最後一个单元了,废话不多说就直接进入正题吧! 抽象(Abstract)类别与方法...