-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminimum_coin_changing.java
69 lines (57 loc) · 1.53 KB
/
minimum_coin_changing.java
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
65
66
67
68
69
package dynamic_programming;
/**
*
* @author shivam
*/
public class minimum_coin_changing {
public static int coinChange(int total,int coins[])
{
int T[]=new int[total+1];
int R[]=new int[total+1];
int i,j;
T[0]=0;
for(i=1;i<=total;i++)
{
T[i]=Integer.MAX_VALUE-1;
}
for(i=1;i<=total;i++)
{
R[i]=0;
}
for(j=0;j<coins.length;j++)
{
for(i=1;i<=total;i++)
{
if(i>=coins[j])
{
if (T[i - coins[j]] + 1 < T[i])
{
T[i]= 1+ T[i-coins[j]];
R[i]=j;
}
}
}
}
printCoinCombination(R,coins);
return T[total];
}
public static void printCoinCombination(int R[],int coins[])
{
if(R[R.length-1]==-1)
System.out.println("NO solution possible");
int start= R.length-1;
System.out.println("Coins to be picked are:");
while(start!=0)
{
int j= R[start];
System.out.print(coins[j]+" ");
start= start-coins[j];
}
System.out.println("");
}
public static void main(String[] args) {
int coins[]= {7,2,3,6}; //assume infinite supply of coins
int total=13;
System.out.println("minimum number of coins to be picked for this total is "+coinChange(total,coins));
}
}