-
Notifications
You must be signed in to change notification settings - Fork 0
/
15. Coin change problem
146 lines (92 loc) · 2.77 KB
/
15. Coin change problem
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
GFG probelm link solution : https://practice.geeksforgeeks.org/problems/coin-change2448/1
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
long long int count(int coins[], int n, int sum) {
// code here.
// done by top down approach
if(sum==0){
return 1;
}
long long int t[n+1][sum+1];
for(int i=0;i<n+1;i++){
for(int j=0;j<sum+1;j++)
{
if(i==0)
{
t[i][j]=0;
}
}
}
for(int i=0;i<n+1;i++){
for(int j=0;j<sum+1;j++){
if(j==0)
{
t[i][j]=1;
}
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(coins[i-1]<=j){
t[i][j]= t[i][j-coins[i-1]] + t[i-1][j];
}
else{
t[i][j]= t[i-1][j];
}
}
}
return t[n][sum];
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int sum, N;
cin >> sum >> N;
int coins[N];
for (int i = 0; i < N; i++) cin >> coins[i];
Solution ob;
cout << ob.count(coins, N, sum) << endl;
}
return 0;
}
// } Driver Code Ends
Leetcode problem link : https://leetcode.com/problems/coin-change-2/
done by memoization
class Solution {
public:
int solve( int n,int target,vector<vector<int>> &dp , vector<int> &coins )
{
if(target==0){
return 1;
}
if(n==0){
return target==0?1:0;
}
if(dp[n][target]!=-1){
return dp[n][target];
}
if(coins[n-1]<=target){
return dp[n][target]= solve(n,target-coins[n-1],dp,coins) + solve(n-1,target,dp,coins);
}
else{
return dp[n][target]=solve(n-1,target,dp,coins);
}
return dp[n][target];
}
int change(int amount, vector<int>& coins) {
// int res=0;
int n= coins.size();
vector<vector<int>> dp (n+1,vector<int> (amount+1,-1));
int ans = solve( n,amount, dp,coins );
return ans ;
}
};