forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0518.cpp
More file actions
25 lines (18 loc) · 708 Bytes
/
Copy path0518.cpp
File metadata and controls
25 lines (18 loc) · 708 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
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<vector<int>> dp(coins.size() + 1, vector<int>(amount + 1, 0));
//fill 1st column by 1, because 0 can always be formed
for(int i = 0; i <= coins.size(); i++)
dp[i][0] = 1;
for(int i = 1; i <= coins.size(); i++){
for(int j = 1; j <= amount; j++){
if(j >= coins[i - 1])
dp[i][j] = dp[i][j - coins[i-1]] + dp[i - 1][j];
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[coins.size()][amount];
}
};