-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinChange2.cpp
More file actions
46 lines (44 loc) · 1.26 KB
/
Copy pathcoinChange2.cpp
File metadata and controls
46 lines (44 loc) · 1.26 KB
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
#include <bits/stdc++.h>
using namespace std;
// Coin Change 2
class Solution
{
public:
int change(int amount, vector<int> &coins)
{
vector<int> dp(amount + 1, 0);
dp[0] = 1;
for (int i = 0; i < coins.size(); i++)
{
for (int j = 1; j < amount + 1; j++)
{
if (j >= coins[i])
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}
};
class memoization
{
public:
long long int changeUtil(int amount, vector<int> &coins, int m, vector<vector<long long int>> &table)
{
//Base Case
// cout << n << " " << m << "\n";
if (amount == 0)
return 1;
if (amount < 0 || m <= 0)
return 0;
if (table[amount][m - 1] != -1)
return table[amount][m - 1];
long long int left = changeUtil(amount, coins, m - 1, table);
long long int right = changeUtil(amount - coins[m - 1], coins, m, table);
return (table[amount][m - 1] = left + right);
}
long long int change(int amount, vector<int> &coins)
{
vector<vector<long long int>> table(amount + 1, vector<long long int>(coins.size(), -1));
return changeUtil(amount, coins, coins.size(), table);
}
};