-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangeProblem.cpp
64 lines (60 loc) · 1.1 KB
/
ChangeProblem.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# include <stdio.h>
# define total_coin 6
int coin[] = {1,2,5,10,20,25};
int changeProblem(int,int,int [][total_coin+1]);
int min(int,int);
int main()
{
int change;
printf("\nEnter the change : ");
scanf("%d",&change);
int memo[change+1][total_coin+1];
for(int i=0; i<=change; i++)
{
for(int j=0; j<=total_coin; j++)
{
memo[i][j] = -1;
}
}
int min_coin = changeProblem(total_coin,change,memo);
printf("\nMinimum coin : %d",min_coin);
return 0;
}
int min(int n,int m)
{
if(n<m)
{
return n;
}
else
{
return m;
}
}
int changeProblem(int i,int j,int memo[][total_coin+1])
{
if(memo[j][i]!=-1)
{
return memo[j][i];
}
if(j==0)
{
return 0;
}
else if(i==0)
{
return coin[5]+1;
}
else if(coin[i-1]<=j)
{
int mn = min(changeProblem(i-1,j,memo),changeProblem(i,j-coin[i-1],memo)+1);
memo[j][i] = mn;
return mn;
}
else
{
int mn = changeProblem(i-1,j,memo);
memo[j][i] = mn;
return mn;
}
}