Skip to content

Commit a573f54

Browse files
committed
fix: update leetcode solution: 0322.Coin Change
更新LeetCode题解
1 parent 31c0526 commit a573f54

File tree

4 files changed

+114
-71
lines changed

4 files changed

+114
-71
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
## 题解
2727
- [LeetCode](/solution/README.md)
28-
- [剑指 Offer(第 2 版)](/lcof/README.md)
29-
- [程序员面试金典(第 6 版)](/lcci/README.md)
28+
- [LeetCode 《剑指 Offer(第 2 版)](/lcof/README.md)
29+
- [LeetCode 《程序员面试金典(第 6 版)](/lcci/README.md)
3030

3131
## 维护者
3232
[Yang Libin](https://github.com/yanglbme): GitHub 技术社区 [@Doocs](https://github.com/doocs) 创建者;[@TheAlgorithms](https://github.com/TheAlgorithms) 组织成员。

solution/0300-0399/0322.Coin Change/README.md

+21-14
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,26 @@
3939
```
4040

4141
### JavaScript
42-
* Dynamic programming:
43-
```JavaScript
44-
var coinChange = function(coins, amount) {
45-
var dp = Array(amount + 1).fill(amount + 1);
46-
dp[0] = 0;
47-
for (var i = 1; i <= amount; i++) {
48-
for (var j = 0; j < coins.length; j++) {
49-
if (coins[j] <= i) {
50-
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
51-
}
52-
}
53-
}
54-
55-
return dp[amount] > amount ? -1 : dp[amount];
42+
动态规划法。
43+
44+
```javascript
45+
/**
46+
* @param {number[]} coins
47+
* @param {number} amount
48+
* @return {number}
49+
*/
50+
var coinChange = function (coins, amount) {
51+
var dp = Array(amount + 1).fill(amount + 1);
52+
dp[0] = 0;
53+
for (var i = 1; i <= amount; i++) {
54+
for (var j = 0; j < coins.length; j++) {
55+
if (coins[j] <= i) {
56+
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
57+
}
58+
}
59+
}
60+
61+
return dp[amount] > amount ? -1 : dp[amount];
5662
};
63+
5764
```
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,79 @@
1-
# [322. Coin Change](https://leetcode.com/problems/coin-change)
2-
3-
## Description
4-
<p>You are given coins of different denominations and a total amount of money <i>amount</i>. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p>
5-
6-
<p><b>Example 1:</b></p>
7-
8-
<pre>
9-
<strong>Input: </strong>coins = <code>[1, 2, 5]</code>, amount = <code>11</code>
10-
<strong>Output: </strong><code>3</code>
11-
<strong>Explanation:</strong> 11 = 5 + 5 + 1</pre>
12-
13-
<p><b>Example 2:</b></p>
14-
15-
<pre>
16-
<strong>Input: </strong>coins = <code>[2]</code>, amount = <code>3</code>
17-
<strong>Output: </strong>-1
18-
</pre>
19-
20-
<p><b>Note</b>:<br />
21-
You may assume that you have an infinite number of each kind of coin.</p>
22-
23-
24-
25-
## Solutions
26-
27-
28-
### Python3
29-
30-
```python
31-
32-
```
33-
34-
### Java
35-
36-
```java
37-
38-
```
39-
40-
### ...
41-
```
42-
43-
```
1+
# [322. Coin Change](https://leetcode.com/problems/coin-change)
2+
3+
## Description
4+
<p>You are given coins of different denominations and a total amount of money <i>amount</i>. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return <code>-1</code>.</p>
5+
6+
7+
8+
<p><b>Example 1:</b></p>
9+
10+
11+
12+
<pre>
13+
14+
<strong>Input: </strong>coins = <code>[1, 2, 5]</code>, amount = <code>11</code>
15+
16+
<strong>Output: </strong><code>3</code>
17+
18+
<strong>Explanation:</strong> 11 = 5 + 5 + 1</pre>
19+
20+
21+
22+
<p><b>Example 2:</b></p>
23+
24+
25+
26+
<pre>
27+
28+
<strong>Input: </strong>coins = <code>[2]</code>, amount = <code>3</code>
29+
30+
<strong>Output: </strong>-1
31+
32+
</pre>
33+
34+
35+
36+
<p><b>Note</b>:<br />
37+
38+
You may assume that you have an infinite number of each kind of coin.</p>
39+
40+
41+
42+
43+
## Solutions
44+
45+
46+
### Python3
47+
48+
```python
49+
50+
```
51+
52+
### Java
53+
54+
```java
55+
56+
```
57+
58+
### JavaScript
59+
Dynamic programming.
60+
61+
```javascript
62+
/**
63+
* @param {number[]} coins
64+
* @param {number} amount
65+
* @return {number}
66+
*/
67+
var coinChange = function (coins, amount) {
68+
var dp = Array(amount + 1).fill(amount + 1);
69+
dp[0] = 0;
70+
for (var i = 1; i <= amount; i++) {
71+
for (var j = 0; j < coins.length; j++) {
72+
if (coins[j] <= i) {
73+
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
74+
}
75+
}
76+
}
77+
78+
return dp[amount] > amount ? -1 : dp[amount];
79+
};

solution/0300-0399/0322.Coin Change/Solution.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
* @param {number} amount
44
* @return {number}
55
*/
6-
var coinChange = function(coins, amount) {
7-
var dp = Array(amount + 1).fill(amount + 1);
8-
dp[0] = 0;
9-
for (var i = 1; i <= amount; i++) {
10-
for (var j = 0; j < coins.length; j++) {
11-
if (coins[j] <= i) {
12-
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
13-
}
14-
}
15-
}
16-
17-
return dp[amount] > amount ? -1 : dp[amount];
6+
var coinChange = function (coins, amount) {
7+
var dp = Array(amount + 1).fill(amount + 1);
8+
dp[0] = 0;
9+
for (var i = 1; i <= amount; i++) {
10+
for (var j = 0; j < coins.length; j++) {
11+
if (coins[j] <= i) {
12+
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
13+
}
14+
}
15+
}
16+
17+
return dp[amount] > amount ? -1 : dp[amount];
1818
};

0 commit comments

Comments
 (0)