Given a set of coins, C = {2,3,5,7}
and a value V = 10
, what is the minimum number of coins I can form from C
to make V
?
Use existing solutions to build up an answer! (A really good video explanation here).
- Create a list
T = [∞] * V
- For every coin
j
in the range0..len(C)
:- For every slot
i
in the range0..len(T)
:- Set the
i
-th item of T to be the minimum of:- What's currently there
- OR
1 + T[i - C[j]]
— aka the item that'sC[j]
positions below the current slot, +1
- Set the
- For every slot
- The value in position
T[V]
is the minimum number of coins required (but does not report which coins to take!)
C = [2,3,5,7]
V = 10
def coins_change(C, V):
# Create a list of size V full of infinity
T = [float('inf')] * V
for j in range(len(C)):
for i in range(len(T)):
current = T[i]
previous = T[i - C[j]]
T[i] = min(current, 1+previous)
return T[V]