-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinchange.py
50 lines (36 loc) · 884 Bytes
/
coinchange.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def min_my_coins(UseCoin, remV):
global coinValue
global max
global dp_memo
if(remV - coinValue[UseCoin] < 0):
return max
if(remV - coinValue[UseCoin] == 0):
return 1
else:
i = 0
remV -= coinValue[UseCoin]
if(dp_memo[remV] == max): #Have not traversed
if(remV == 1):
print("1 has not been traversed")
while(i < len(coinValue)):
curr_coin = 1 + min_my_coins(i, remV)
if(dp_memo[remV] > curr_coin):
dp_memo[remV] = curr_coin
i += 1
return dp_memo[remV]
if __name__ == "__main__":
V = 7
n = 3
coinValue = [1, 2, 5]
max = V + 1
dp_memo = [max for i in range(V+1)]
i = 0
answer = max
while(i < len(coinValue)):
min_coins = min_my_coins(i, V)
if(answer > min_coins):
answer = min_coins
dp_memo[V] = answer
i += 1
print(answer)
print(dp_memo)