Skip to content

Commit 08461ab

Browse files
authored
Ways of Coin Change | Bottom-up | O(N*M) time with O(N) extra space
1 parent b3c481a commit 08461ab

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Dynamic-Programming/change.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int change(int amount, vector<int>& coins) {
4+
int n = coins.size();
5+
vector<int> dp(amount+1); // bottom-up approach
6+
dp[0] = 1; // because only way if no change
7+
for(int i = 0; i < n; ++i) {
8+
for(int j = 1; j <amount+1; ++j) {
9+
if (coins[i] <= j) {
10+
dp[j] += dp[j - coins[i]];
11+
}
12+
}
13+
}
14+
return dp[amount];
15+
}
16+
};

0 commit comments

Comments
 (0)