forked from spaghetti-source/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_coin_change.cc
More file actions
66 lines (58 loc) · 1.78 KB
/
minimum_coin_change.cc
File metadata and controls
66 lines (58 loc) · 1.78 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
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
// Minimum Coin Change problem (Dynamic Programming)
// Description:
// Given an infinite supply of valued coins {c1, c2, c3, ... cn} and a value N
// Output the minimum number of coins to make the change
// Algorithm:
// Construct a table[][] and solve in a top bottom manner.
// The witness pred[] array records the coins that are selected.
// define d[i] as the minimum number of coins to change i amout of money, cj as the coin value of jth coin
// then
// d[i] = 0, when i=0
// d[i] = min(d[i], d[i-cj]+1) for cj<=i, when i>=1
// Complexity:
// For m number of coins and n amount of money:
// O(mn) for time
// O(n) for space
#include <iostream>
#include <vector>
#include <cmath>
#include <climits>
using namespace std;
// find the minimum number of coin changes and print the witness
int coin_change(int coins[], int cn, int money){
int table[money+1];
table[0] = 0;
int pred[money+1];
for (int i=0; i<=money;i++){
pred[i] = 0;
}
for (int j=1; j<=money;j++){
table[j] = INT_MAX;
}
for (int i=1;i<=money;i++){
int mini = table[i];
for (int j=0; j<cn;j++){
if (i >= coins[j]){
mini = min(mini, table[i-coins[j]]+1);
pred[i] = j;
}
}
table[i] = mini;
}
int m = money;
while (m != 0){
cout<<"change coin: "<<coins[pred[m]]<<" ";
m = m - coins[pred[m]];
}
cout<<endl;
return table[money];
}
int main(){
int coins1[] = {1, 5, 10, 25};
cout<<coin_change(coins1, sizeof(coins1)/sizeof(coins1[0]), 20)<<" coins"<<endl;
cout<<coin_change(coins1, sizeof(coins1)/sizeof(coins1[0]), 7)<<" coins"<<endl;
int coins2[] = {1,5,7,15};
cout<<coin_change(coins2, sizeof(coins2)/sizeof(coins2[0]), 8)<<" coins"<<endl;
cout<<coin_change(coins2, sizeof(coins2)/sizeof(coins2[0]), 20)<<" coins"<<endl;
return 0;
}