-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumCoinChange_TopDown.cpp
More file actions
62 lines (52 loc) · 1.7 KB
/
Copy pathminimumCoinChange_TopDown.cpp
File metadata and controls
62 lines (52 loc) · 1.7 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <stack>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <sstream>
#include <iomanip>
using namespace std;
vector<int> coins = {7, 10};
vector<int>dp(100, INT_MAX - 10);
int solve(int n){
//The base condition would have if the total amount left is 0 for which we would need 0 number of coins
if(n <= 0)
return 0;
//We use memoization to make sure we dont have to calculate the same values again by checking if we have already calculated the value or not
if(dp[n] != INT_MAX - 10)
return dp[n];
//Checking for minimum number of coins when we consider all the coins one by one and including the coin if the total - currency >= 0 and than chosing the
//minimum number of coins after that
for(int i = 0 ; i < coins.size() ; i++){
if(n - coins[i] >= 0)
dp[n] = min(dp[n], solve(n-coins[i]) + 1);
}
return dp[n];
}
int32_t main()
{
int n = 19;
//The below code provide as a check to see if we can actually calculate the value by using the types of currency we are provided
//We do that by checking against the condition of how many coins will be required if the currency would have been 1, in that case the maximum answer can be the total
//itself and if it is not than that means that it is not possible at all
if(solve(n) > n)
cout<<-1<<endl;
else
cout<<solve(n);
}