-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_change.py
More file actions
30 lines (26 loc) · 802 Bytes
/
coin_change.py
File metadata and controls
30 lines (26 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sys
memo = {}
def change_recursion(coins, amount):
if amount == 0:
return 0
if amount < 0:
return -1
if amount in memo:return memo[amount]
res = sys.maxsize
for coin in coins:
sub_problem = change_recursion(coins, amount - coin)
if sub_problem == -1:continue
res = min(res, sub_problem+1)
memo[amount] = -1 if res==sys.maxsize else res
return memo[amount]
def change_dp(coins, amount):
dp = {0:0}
amount_max = amount+1
for i in range(1, amount_max):
m=amount_max
for coin in coins:
if i - coin < 0: continue
dp[i] = min(m, 1 + dp[i - coin])
return (dp[amount] == amount_max) and -1 or dp[amount]
# print(change_recursion([1, 2, 5], 20))
print(change_dp([1, 2, 5], 7))