forked from Hemanth5603/DP---DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoin_change_1.cpp
More file actions
31 lines (25 loc) · 806 Bytes
/
coin_change_1.cpp
File metadata and controls
31 lines (25 loc) · 806 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
#include<bits/stdc++.h>
using namespace std;
class Solution {
public:
int solve(int ind, int t, vector<int> &coins, vector<vector<int>> &dp){
if(ind == 0){
if(t % coins[ind] == 0) return t/coins[0];
return 1e9;
}
if(dp[ind][t] != -1) return dp[ind][t];
int npick = solve(ind -1, t, coins, dp);
int pick = INT_MAX;
if(coins[ind] <= t){
pick = 1 + solve(ind, t - coins[ind], coins, dp);
}
return dp[ind][t] = min(npick, pick);
}
int coinChange(vector<int>& coins, int amount) {
int n = coins.size();
vector<vector<int>> dp(n+1, vector<int>(amount + 1, -1));
int ans = solve(n-1, amount, coins, dp);
if(ans >= 1e9) return -1;
else return ans;
}
};