[LeetCode] Coin Change 2

518. Coin Change 2

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the number of combinations that make up that amount. If that amount of money cannot be made up by any combination of the coins, return 0.

You may assume that you have an infinite number of each kind of coin.

The answer is guaranteed to fit into a signed 32-bit integer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1, 0);
dp[0] = 1;
for(auto& c : coins) {
for(int i = c; i <= amount; i++) {
dp[i] += dp[i - c];
}
}

return dp[amount];
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/05/15/PS/LeetCode/coin-change-2/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.