Skip to content

Commit ae2f00b

Browse files
committed
Working and in DP
1 parent 35ce59b commit ae2f00b

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

CoinChangeSet2.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#Find minimum number of coins that make a given value
2+
#Given a value V, if we want to make change for V cents, and we have infinite supply
3+
#of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
4+
def createMatrix(n, s):
5+
m = [[ 0 for col in range(n+1)] for row in range(len(s)+1)]
6+
return m
7+
8+
def foo(n, s):
9+
matrix = createMatrix(n,s)
10+
for row in range(len(s)):
11+
for col in range(n+1):
12+
if(s[row] == col):
13+
matrix[row+1][col] = 1
14+
elif(s[row] > col):
15+
#Copy the value from top
16+
matrix[row+1][col] = matrix[row][col]
17+
else:
18+
#Get the value from those many steps behind
19+
value = matrix[row+1][col - s[row]]
20+
#If this value is 0 then get the value form top
21+
if value == 0:
22+
matrix[row+1][col] = matrix[row][col]
23+
if(value > 0):
24+
#Find top value
25+
top = matrix[row][col]
26+
if top == 0:
27+
matrix[row+1][col] = value+1
28+
else:
29+
matrix[row+1][col] = min(top, value+1)
30+
print matrix
31+
print "we need a minimum of "+str(matrix[len(s)][n])
32+
33+
#Main Program
34+
foo(10, [2,5,3,6])
35+
foo(30, [25, 10, 5])
36+
foo(11, [9, 6, 5, 1])

0 commit comments

Comments
 (0)