forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0322.cpp
More file actions
33 lines (24 loc) · 890 Bytes
/
0322.cpp
File metadata and controls
33 lines (24 loc) · 890 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
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
//dp with coins as rows and amount as columns
vector<vector<int>> dp(coins.size() + 1, vector<int>(amount + 1, 0));
for(int i = 0; i <= coins.size(); i++){
for(int j = 0; j <= amount; j++){
if(j == 0)
dp[i][j] = 0;
else if(i == 0)
dp[i][j] = 1e5;
else if(coins[i - 1] > j)
dp[i][j] = dp[i - 1][j];
else
dp[i][j] = min(1 + dp[i][j - coins[i-1]], dp[i - 1][j]);
}
}
int ans = dp[coins.size()][amount];
if(ans > 1e4)
return -1;
else
return ans;
}
};