-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinChangeMinCoin.cpp
More file actions
50 lines (48 loc) · 990 Bytes
/
CoinChangeMinCoin.cpp
File metadata and controls
50 lines (48 loc) · 990 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
41
42
43
44
45
46
47
48
49
50
#include <bits/stdc++.h>
using namespace std;
int CoinChangeMinCoin(int coin[], int n, int sum)
{
int dp[n+1][sum+1];
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(i==0)
{
dp[i][j] = INT_MAX-1;
}
if(j==0 && i>0)
{
dp[i][j] = 0;
}
}
}
for(int j=1;j<sum+1;j++)
{
if(j%coin[0] == 0)
{
dp[1][j] = j/coin[0];
} else {
dp[1][j] = INT_MAX-1;
}
}
for(int i=2;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(coin[i-1]<=j)
{
dp[i][j] = min(dp[i][j-coin[i-1]]+1, dp[i-1][j]);
} else {
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n][sum];
}
int main() {
int n = 4;
int coin[n]= {9, 6, 5, 1};
int sum = 11;
cout<<CoinChangeMinCoin(coin, n, sum)<<endl;
}