-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTheCoinChangeProblem.cpp
More file actions
37 lines (32 loc) · 935 Bytes
/
TheCoinChangeProblem.cpp
File metadata and controls
37 lines (32 loc) · 935 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
//The Coin Change Problem
//Given an Array of coins C, and money S, print number of ways to make a change for S
//Eg: A=[1,2] S=4 answer is {1,1,1,1}, {2,2}, {2,1,1} i.e 3 ways
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3;
int A[N], n;
int vis[N][N], dp[N][N];
int ways(int pos, int S) {
if(pos == n) return S == 0;
int &ans = dp[pos][S];
if(vis[pos][S]) return ans; vis[pos][S] = 1, ans = 0;
int times = 0;
while(times*A[pos] <= S) ans += ways(pos+1, S-times*A[pos]), times++;
return ans;
}
//Faster than ways function as it causes only two transitions
int fasterWays(int pos, int S) {
if(pos == n) return S == 0;
if(S < 0) return 0;
int &ans = dp[pos][S];
if(vis[pos][S]) return ans; vis[pos][S] = 1, ans = 0;
ans = ways(pos, S-A[pos]) + ways(pos+1, S);
return ans;
}
int main() {
int i, S;
cin >> n >> S;
for(i=0; i<n; i++) cin >> A[i];
cout << fasterWays(0, S) << endl;
return 0;
}