-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_change.cpp
46 lines (45 loc) · 1.28 KB
/
coin_change.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
// Problem Link - https://leetcode.com/problems/coin-change-2/
/* Complete Problem Solution
class Solution {
public:
int change(int amount, vector<int>& coins) {
int size = coins.size();
vector<vector<int>> dp(size+1, vector<int> (amount+1));
for (int i = 0; i <= size; i++)
{
for (int j = 0; j <= amount; j++)
{
if(j == 0) dp[i][j] = 1;
else if(i == 0) dp[i][j] = 0;
else if(coins[i-1] <= j) dp[i][j] = dp[i][j-coins[i-1]] + dp[i-1][j];
else if(coins[i-1] > j) dp[i][j] = dp[i-1][j];
}
}
return dp[size][amount];
}
};
*/
#include <bits/stdc++.h>
using namespace std;
int coinChange(vector<int> coins, int sum)
{
int size = coins.size();
vector<vector<int>> dp(size+1, vector<int> (sum+1));
for (int i = 0; i <= size; i++)
{
for (int j = 0; j <= sum; j++)
{
if(j == 0) dp[i][j] = 1;
else if(i == 0) dp[i][j] = 0;
else if(coins[i-1] <= j) dp[i][j] = dp[i][j-coins[i-1]] + dp[i-1][j];
else if(coins[i-1] > j) dp[i][j] = dp[i-1][j];
}
}
return dp[size][sum];
}
int main()
{
vector<int> coins = {2};
int sum = 3;
cout<<coinChange(coins, sum);
}