forked from sunidhi2001/Hacktoberfest2022-accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coin_change.cpp
36 lines (33 loc) · 937 Bytes
/
Coin_change.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
#include <bits/stdc++.h>
using namespace std;
int coinChange(vector<int>& coins, int amount) {
int size= coins.size();
vector<vector<long long>> dp(size+1 , vector<long long>(amount+1,0));
for(int i=0 ;i<=size;i++)
{
dp[i][0]=0;
}
for(int j=0 ;j<=amount;j++)
{
dp[0][j]=INT_MAX;
}
for(int i=1 ;i<=size;i++)
{
for(int j=1 ;j<=amount ;j++)
{
if(coins[i-1]>j)
dp[i][j]=dp[i-1][j];
else
dp[i][j]=min(dp[i-1][j],1+dp[i][j-coins[i-1]]);
}
}
if(dp[size][amount]==INT_MAX) return -1;
return dp[size][amount];
}
int main()
{
vector<int>coins = {1,2,5};
int amount = 11;
cout<<"Minimun number of coins required to obtain "<<amount<<" is "<<coinChange(coins, amount)<<endl;
return 0;
}