-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay30.cpp
43 lines (34 loc) · 893 Bytes
/
Day30.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
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter the number of coin denominations: ";
cin >> n;
int coins[n];
cout << "Enter the coin denominations: ";
for (int i = 0; i < n; i++) {
cin >> coins[i];
}
int amount;
cout << "Enter the amount: ";
cin >> amount;
int max_value = amount + 1;
int dp[amount + 1];
for (int i = 0; i <= amount; i++) {
dp[i] = max_value;
}
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < n; j++) {
if (coins[j] <= i) {
dp[i] = (dp[i] < dp[i - coins[j]] + 1) ? dp[i] : (dp[i - coins[j]] + 1);
}
}
}
if (dp[amount] > amount) {
cout << -1;
} else {
cout << dp[amount];
}
return 0;
}