Skip to content

Commit efd1993

Browse files
committed
feat: add solutions to leetcode problem: No.0322. Coin Change
1 parent 09a0eb6 commit efd1993

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

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

+24-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
<pre>
1818
<strong>输入:</strong>coins = <code>[1, 2, 5]</code>, amount = <code>11</code>
19-
<strong>输出:</strong><code>3</code>
19+
<strong>输出:</strong><code>3</code>
2020
<strong>解释:</strong>11 = 5 + 5 + 1</pre>
2121

2222
<p><strong>示例 2:</strong></p>
@@ -61,28 +61,47 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
类似完全背包的思路,硬币数量不限,求凑成总金额所需的最少的硬币个数。
65+
6466
<!-- tabs:start -->
6567

6668
### **Python3**
6769

6870
<!-- 这里可写当前语言的特殊实现逻辑 -->
6971

7072
```python
71-
73+
class Solution:
74+
def coinChange(self, coins: List[int], amount: int) -> int:
75+
dp = [amount + 1 for i in range(amount + 1)]
76+
dp[0] = 0
77+
for coin in coins:
78+
for j in range(coin, amount + 1):
79+
dp[j] = min(dp[j], dp[j - coin] + 1)
80+
return -1 if dp[amount] > amount else dp[amount]
7281
```
7382

7483
### **Java**
7584

7685
<!-- 这里可写当前语言的特殊实现逻辑 -->
7786

7887
```java
79-
88+
class Solution {
89+
public int coinChange(int[] coins, int amount) {
90+
int[] dp = new int[amount + 1];
91+
Arrays.fill(dp, amount + 1);
92+
dp[0] = 0;
93+
for (int coin : coins) {
94+
for (int j = coin; j <= amount; j++) {
95+
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
96+
}
97+
}
98+
return dp[amount] > amount ? -1 : dp[amount];
99+
}
100+
}
80101
```
81102

82103
### **JavaScript**
83104

84-
动态规划法。
85-
86105
```js
87106
/**
88107
* @param {number[]} coins

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,43 @@
5959

6060
## Solutions
6161

62+
Similar to the idea of ​​a complete backpack, there is no limit to the number of coins. Find the minimum number of coins required to make up the total amount.
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

6668
```python
67-
69+
class Solution:
70+
def coinChange(self, coins: List[int], amount: int) -> int:
71+
dp = [amount + 1 for i in range(amount + 1)]
72+
dp[0] = 0
73+
for coin in coins:
74+
for j in range(coin, amount + 1):
75+
dp[j] = min(dp[j], dp[j - coin] + 1)
76+
return -1 if dp[amount] > amount else dp[amount]
6877
```
6978

7079
### **Java**
7180

7281
```java
73-
82+
class Solution {
83+
public int coinChange(int[] coins, int amount) {
84+
int[] dp = new int[amount + 1];
85+
Arrays.fill(dp, amount + 1);
86+
dp[0] = 0;
87+
for (int coin : coins) {
88+
for (int j = coin; j <= amount; j++) {
89+
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
90+
}
91+
}
92+
return dp[amount] > amount ? -1 : dp[amount];
93+
}
94+
}
7495
```
7596

7697
### **JavaScript**
7798

78-
Dynamic programming.
79-
8099
```js
81100
/**
82101
* @param {number[]} coins
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
class Solution {
2-
public int coinChange(int[] coins, int amount) {
3-
int n = coins.length;
4-
int[] dp = new int[amount + 1];
5-
Arrays.fill(dp, amount + 1);
6-
dp[0] = 0;
7-
for (int i = 1; i <= amount; i++) {
8-
for (int j = 0; j < n; j++) {
9-
if (i >= coins[j]) {
10-
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
11-
}
12-
}
13-
}
14-
return dp[amount] > amount ? -1 : dp[amount];
15-
}
16-
}
2+
public int coinChange(int[] coins, int amount) {
3+
int[] dp = new int[amount + 1];
4+
Arrays.fill(dp, amount + 1);
5+
dp[0] = 0;
6+
for (int coin : coins) {
7+
for (int j = coin; j <= amount; j++) {
8+
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
9+
}
10+
}
11+
return dp[amount] > amount ? -1 : dp[amount];
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def coinChange(self, coins: List[int], amount: int) -> int:
3+
dp = [amount + 1 for i in range(amount + 1)]
4+
dp[0] = 0
5+
for coin in coins:
6+
for j in range(coin, amount + 1):
7+
dp[j] = min(dp[j], dp[j - coin] + 1)
8+
return -1 if dp[amount] > amount else dp[amount]

0 commit comments

Comments
 (0)