Skip to content

Commit 56c6a85

Browse files
committed
0518-coin-change-ii.md Added 7 languages' solutions.
1 parent d66f237 commit 56c6a85

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ After finishing one category of questions, you can study another category to imp
2323
- [392. Is Subsequence](problems/0392-is-subsequence.md)
2424
- [583. Delete Operation for Two Strings](problems/0583-delete-operation-for-two-strings.md)
2525
- [72. Edit Distance](problems/0072-edit-distance.md)
26+
27+
### Knapsack problems
2628
- [416. Partition Equal Subset Sum](problems/0416-partition-equal-subset-sum.md)
2729
- [1049. Last Stone Weight II](problems/1049-last-stone-weight-ii.md)
2830
- [494. Target Sum](problems/0494-target-sum.md)
2931
- [474. Ones and Zeroes](problems/0474-ones-and-zeroes.md)
32+
- [518. Coin Change II](problems/0518-coin-change-ii.md)
3033

3134
- More LeetCode problems will be added soon...

problems/0518-coin-change-ii.md

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# 518. Coin Change II
2+
LeetCode problem: [518. Coin Change II](https://leetcode.com/problems/coin-change-ii/)
3+
4+
## LeetCode problem description
5+
> You are given an integer array `coins` representing coins of different denominations and an integer amount representing a total `amount` of money.
6+
7+
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`.
8+
9+
You may assume that you have an infinite number of each kind of coin.
10+
11+
The answer is **guaranteed** to fit into a signed 32-bit integer.
12+
13+
```
14+
Example 1:
15+
16+
Input: amount = 5, coins = [1,2,5]
17+
Output: 4
18+
19+
Explanation: there are four ways to make up the amount:
20+
5=5
21+
5=2+2+1
22+
5=2+1+1+1
23+
5=1+1+1+1+1
24+
------------------------------------------------------------------------
25+
26+
Example 2:
27+
28+
Input: amount = 3, coins = [2]
29+
Output: 0
30+
31+
Explanation: the amount of 3 cannot be made up just with coins of 2.
32+
------------------------------------------------------------------------
33+
34+
Example 3:
35+
36+
Input: amount = 10, coins = [10]
37+
Output: 1
38+
------------------------------------------------------------------------
39+
40+
Constraints:
41+
42+
1 <= coins.length <= 300
43+
1 <= coins[i] <= 5000
44+
All the values of 'coins' are unique.
45+
0 <= amount <= 5000
46+
```
47+
48+
## Thoughts
49+
It is a `Unbounded Knapsack Problem`.
50+
51+
### Complexity
52+
* Time: `O(n * m)`.
53+
* Space: `O(n)`.
54+
55+
## Python
56+
```python
57+
class Solution:
58+
def change(self, amount: int, coins: List[int]) -> int:
59+
dp = [0] * (amount + 1)
60+
dp[0] = 1
61+
62+
for coin in coins:
63+
for j in range(coin, len(dp)):
64+
dp[j] += dp[j - coin]
65+
66+
return dp[-1]
67+
```
68+
69+
## C++
70+
```cpp
71+
class Solution {
72+
public:
73+
int change(int amount, vector<int>& coins) {
74+
auto dp = vector<unsigned int>(amount + 1, 0);
75+
dp[0] = 1;
76+
77+
for (auto coin : coins) {
78+
for (auto j = coin; j < dp.size(); j++) {
79+
dp[j] += dp[j - coin];
80+
}
81+
}
82+
83+
return dp.back();
84+
}
85+
};
86+
```
87+
88+
## Java
89+
```java
90+
class Solution {
91+
public int change(int amount, int[] coins) {
92+
var dp = new int[amount + 1];
93+
dp[0] = 1;
94+
95+
for (var coin : coins) {
96+
for (var j = coin; j < dp.length; j++) {
97+
dp[j] += dp[j - coin];
98+
}
99+
}
100+
101+
return dp[dp.length - 1];
102+
}
103+
}
104+
```
105+
106+
## C#
107+
```c#
108+
public class Solution {
109+
public int Change(int amount, int[] coins) {
110+
var dp = new int[amount + 1];
111+
dp[0] = 1;
112+
113+
foreach (var coin in coins) {
114+
for (var j = coin; j < dp.Length; j++) {
115+
dp[j] += dp[j - coin];
116+
}
117+
}
118+
119+
return dp.Last();
120+
}
121+
}
122+
```
123+
124+
## JavaScript
125+
```javascript
126+
var change = function(amount, coins) {
127+
const dp = Array(amount + 1).fill(0)
128+
dp[0] = 1
129+
130+
for (const coin of coins) {
131+
for (let j = coin; j < dp.length; j++) {
132+
dp[j] += dp[j - coin]
133+
}
134+
}
135+
136+
return dp.at(-1);
137+
};
138+
```
139+
140+
## Go
141+
```go
142+
func change(amount int, coins []int) int {
143+
dp := make([]int, amount + 1)
144+
dp[0] = 1
145+
146+
for _, coin := range coins {
147+
for j := coin; j < len(dp); j++ {
148+
dp[j] += dp[j - coin]
149+
}
150+
}
151+
152+
return dp[len(dp) - 1]
153+
}
154+
```
155+
156+
## Ruby
157+
```ruby
158+
def change(amount, coins)
159+
dp = Array.new(amount + 1, 0)
160+
dp[0] = 1
161+
162+
coins.each do |coin|
163+
(coin...dp.size).each do |j|
164+
dp[j] += dp[j - coin]
165+
end
166+
end
167+
168+
dp[-1]
169+
end
170+
```

0 commit comments

Comments
 (0)