Skip to content

Commit

Permalink
feat: add solutions to leetcode problem: No.0322. Coin Change
Browse files Browse the repository at this point in the history
  • Loading branch information
maolonglong committed May 2, 2021
1 parent 09a0eb6 commit efd1993
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
29 changes: 24 additions & 5 deletions solution/0300-0399/0322.Coin Change/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

<p><strong>示例 2:</strong></p>
Expand Down Expand Up @@ -61,28 +61,47 @@

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

类似完全背包的思路,硬币数量不限,求凑成总金额所需的最少的硬币个数。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [amount + 1 for i in range(amount + 1)]
dp[0] = 0
for coin in coins:
for j in range(coin, amount + 1):
dp[j] = min(dp[j], dp[j - coin] + 1)
return -1 if dp[amount] > amount else dp[amount]
```

### **Java**

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

```java

class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int coin : coins) {
for (int j = coin; j <= amount; j++) {
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}
```

### **JavaScript**

动态规划法。

```js
/**
* @param {number[]} coins
Expand Down
27 changes: 23 additions & 4 deletions solution/0300-0399/0322.Coin Change/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,43 @@

## Solutions

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.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [amount + 1 for i in range(amount + 1)]
dp[0] = 0
for coin in coins:
for j in range(coin, amount + 1):
dp[j] = min(dp[j], dp[j - coin] + 1)
return -1 if dp[amount] > amount else dp[amount]
```

### **Java**

```java

class Solution {
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int coin : coins) {
for (int j = coin; j <= amount; j++) {
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}
```

### **JavaScript**

Dynamic programming.

```js
/**
* @param {number[]} coins
Expand Down
27 changes: 12 additions & 15 deletions solution/0300-0399/0322.Coin Change/Solution.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
class Solution {
public int coinChange(int[] coins, int amount) {
int n = coins.length;
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int i = 1; i <= amount; i++) {
for (int j = 0; j < n; j++) {
if (i >= coins[j]) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}
public int coinChange(int[] coins, int amount) {
int[] dp = new int[amount + 1];
Arrays.fill(dp, amount + 1);
dp[0] = 0;
for (int coin : coins) {
for (int j = coin; j <= amount; j++) {
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
}
8 changes: 8 additions & 0 deletions solution/0300-0399/0322.Coin Change/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [amount + 1 for i in range(amount + 1)]
dp[0] = 0
for coin in coins:
for j in range(coin, amount + 1):
dp[j] = min(dp[j], dp[j - coin] + 1)
return -1 if dp[amount] > amount else dp[amount]

0 comments on commit efd1993

Please sign in to comment.