-
Notifications
You must be signed in to change notification settings - Fork 186
/
coinchange.cpp
51 lines (44 loc) · 994 Bytes
/
coinchange.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
44
45
46
47
48
49
50
51
#include<iostream>
using namespace std;
///Coin Change Problem
///Top Down DP
int dp[100] = {0};
int coinWaysTD(int amt,int coins[],int n){
if(amt==0){
dp[0] = 1;
return 1;
}
///Recursive Case
int ans = 0;
if(dp[amt]!=0){
return dp[amt];
}
for(int i=0;i<n;i++){
if(amt-coins[i]>=0){
ans += coinWaysTD(amt-coins[i],coins,n);
}
}
///Store and return for the first time
dp[amt] = ans;
return ans;
}
///Bottom Up DP
int coinWaysBU(int amt,int coins[],int n){
int dp[100] = {0};
dp[0] = 1;
for(int i=1;i<=amt;i++){
for(int j=0;j<n;j++){
if(i-coins[j]>=0){
dp[i] += dp[i-coins[j]];
}
}
}
return dp[amt];
}
int main(){
int coins[] = {1,2,3,8};
int amount = 3;
cout<<coinWaysTD(amount,coins,4)<<endl;
cout<<coinWaysBU(amount,coins,4)<<endl;
return 0;
}