Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 5

Coin Change

Dynamic Programming
Problem: Find minimum number of
coins that make a given value
• Make a change, Total = 13
• Available Coin Set = { 6, 2, 7, 3 }

T[i] [j] is the 2D array to denote the minimum number of coins require to make the change
Coin[i] is the sorted array of available coin sets. [Index Start from 1]

• If V == 0, then 0 coins required.


T[i][j] = 0
• If V > 0
Case 1: T [ i ] [ j ] = T [ i-1 ] [ j ] If Coin[ i ] is not takes
Case 2: T [ i ] [ j ] = 1 + T [i] [ j – Coin [ i ] ] If Coin[ i ] is taken
0 1 2 3 4
Coin[ i ] = 2 3 6 7

T [i][j] = MIN ( T [i-1][j] , 1 + T[i][j-Coin[i] )

0 1 2 3 4 5 6 7 8 9 10 11 12 13

7
0 1 2 3 4
Coin[ i ] = 2 3 6 7

T [i][j] = MIN ( T [i-1] [j] , 1 + T[i] [j-Coin[i] )

0 1 2 3 4 5 6 7 8 9 10 11 12 13
0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
2 0 ∞ 1 ∞ 2 ∞ 3 ∞ 4 ∞ 5 ∞ 6 ∞
3 0 ∞ 1 1 2 2 2 3 3 3 4 4 4 5
6 0 ∞ 1 1 2 2 1 3 2 2 3 3 2 4
7 0 ∞ 1 1 2 2 1 1 2 2 2 3 2 2
Result:

Number of Coins requires to make a change to 13 = 2


Coin Set = {7, 6}

You might also like