Skip to content

Commit 33eb15a

Browse files
Coin Change 2
1 parent af310a0 commit 33eb15a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

7.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
3+
Coin Change 2
4+
-------------
5+
6+
You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
7+
8+
Example 1:
9+
10+
Input: amount = 5, coins = [1, 2, 5]
11+
Output: 4
12+
Explanation: there are four ways to make up the amount:
13+
5=5
14+
5=2+2+1
15+
5=2+1+1+1
16+
5=1+1+1+1+1
17+
Example 2:
18+
19+
Input: amount = 3, coins = [2]
20+
Output: 0
21+
Explanation: the amount of 3 cannot be made up just with coins of 2.
22+
Example 3:
23+
24+
Input: amount = 10, coins = [10]
25+
Output: 1
26+
27+
Note:
28+
29+
You can assume that
30+
31+
0 <= amount <= 5000
32+
1 <= coin <= 5000
33+
the number of coins is less than 500
34+
the answer is guaranteed to fit into signed 32-bit integer
35+
36+
*/
37+
38+
class Solution {
39+
public:
40+
int change(int amount, vector<int>& coins) {
41+
int values[amount+1];
42+
memset(values, 0, sizeof(values));
43+
values[0] = 1;
44+
for(int i=0; i<coins.size(); i++) {
45+
for(int j=coins[i]; j<=amount; j++) {
46+
values[j] += values[j - coins[i]];
47+
}
48+
}
49+
return values[amount];
50+
}
51+
};

0 commit comments

Comments
 (0)