forked from agrawalKuber/Hacktoberfest_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_change_min.cpp
More file actions
40 lines (36 loc) · 753 Bytes
/
coin_change_min.cpp
File metadata and controls
40 lines (36 loc) · 753 Bytes
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
//Coin change minimum coins required to give change
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> coin, int n, int sum, vector<vector<int>> &t){
for(int i=2; i<n+1; i++){
for(int j=1; j<sum+1; j++){
if(coin[i-1]<=j){
t[i][j] = min(1+t[i][j-coin[i-1]], t[i-1][j]);
}
else{
t[i][j] = t[i-1][j];
}
}
}
return t[n][sum];
}
int main(){
int n = 2;
vector<int> coin = {1,2};
int sum = 2;
vector<vector<int>> t(n+1, vector<int> (sum+1, INT_MAX-1));
for(int i=1; i<n+1; i++){
t[i][0] = 0;
}
//Initialise 2nd row from index 1 manually.
for(int j=1; j<sum+1; j++){
if(j%coin[0]==0){
t[1][j] = j/coin[0];
}
else{
t[1][j] = INT_MAX-1;
}
}
cout<<solve(coin, n, sum, t)<<"\n";
return 0;
}