forked from PriyankaKhire/ProgrammingPracticePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChangeSet2.py
More file actions
36 lines (34 loc) · 1.34 KB
/
CoinChangeSet2.py
File metadata and controls
36 lines (34 loc) · 1.34 KB
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
#Find minimum number of coins that make a given value
#Given a value V, if we want to make change for V cents, and we have infinite supply
#of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum number of coins to make the change?
def createMatrix(n, s):
m = [[ 0 for col in range(n+1)] for row in range(len(s)+1)]
return m
def foo(n, s):
matrix = createMatrix(n,s)
for row in range(len(s)):
for col in range(n+1):
if(s[row] == col):
matrix[row+1][col] = 1
elif(s[row] > col):
#Copy the value from top
matrix[row+1][col] = matrix[row][col]
else:
#Get the value from those many steps behind
value = matrix[row+1][col - s[row]]
#If this value is 0 then get the value form top
if value == 0:
matrix[row+1][col] = matrix[row][col]
if(value > 0):
#Find top value
top = matrix[row][col]
if top == 0:
matrix[row+1][col] = value+1
else:
matrix[row+1][col] = min(top, value+1)
print matrix
print "we need a minimum of "+str(matrix[len(s)][n])
#Main Program
foo(10, [2,5,3,6])
foo(30, [25, 10, 5])
foo(11, [9, 6, 5, 1])