Skip to content

Commit d24c7fd

Browse files
committed
feat: update solutions to leetcode problem: No.0322. Coin Change
1 parent 33a7dcc commit d24c7fd

File tree

9 files changed

+65
-52
lines changed

9 files changed

+65
-52
lines changed

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

+10-19
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
```python
7373
class Solution:
7474
def coinChange(self, coins: List[int], amount: int) -> int:
75-
dp = [amount + 1 for i in range(amount + 1)]
75+
dp = [amount + 1] * (amount + 1)
7676
dp[0] = 0
7777
for coin in coins:
7878
for j in range(coin, amount + 1):
@@ -109,16 +109,13 @@ class Solution {
109109
* @return {number}
110110
*/
111111
var coinChange = function (coins, amount) {
112-
var dp = Array(amount + 1).fill(amount + 1);
112+
let dp = Array(amount + 1).fill(amount + 1);
113113
dp[0] = 0;
114-
for (var i = 1; i <= amount; i++) {
115-
for (var j = 0; j < coins.length; j++) {
116-
if (coins[j] <= i) {
117-
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
118-
}
114+
for (const coin of coins) {
115+
for (let j = coin; j <= amount; ++j) {
116+
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
119117
}
120118
}
121-
122119
return dp[amount] > amount ? -1 : dp[amount];
123120
};
124121
```
@@ -129,20 +126,14 @@ var coinChange = function (coins, amount) {
129126
class Solution {
130127
public:
131128
int coinChange(vector<int>& coins, int amount) {
132-
std::vector<int> dp(amount + 1, -1);
129+
vector<int> dp(amount + 1, amount + 1);
133130
dp[0] = 0;
134-
for (int i = 1; i <= amount; i++) {
135-
for (int j = 0; j < coins.size(); j++) {
136-
if (coins[j] <= i && dp[i - coins[j]] != -1) {
137-
// 当 当前值未被计算,或者有更小的组成方式的情况下
138-
if (dp[i] == -1 || dp[i] > dp[i - coins[j]] + 1) {
139-
dp[i] = dp[i - coins[j]] + 1;
140-
}
141-
}
131+
for (auto coin : coins) {
132+
for (int j = coin; j <= amount; ++j) {
133+
dp[j] = min(dp[j], dp[j - coin] + 1);
142134
}
143135
}
144-
145-
return dp[amount];
136+
return dp[amount] > amount ? -1 : dp[amount];
146137
}
147138
};
148139
```

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

+25-10
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Similar to the idea of ​​a complete backpack, there is no limit to the numbe
6868
```python
6969
class Solution:
7070
def coinChange(self, coins: List[int], amount: int) -> int:
71-
dp = [amount + 1 for i in range(amount + 1)]
71+
dp = [amount + 1] * (amount + 1)
7272
dp[0] = 0
7373
for coin in coins:
7474
for j in range(coin, amount + 1):
@@ -103,18 +103,33 @@ class Solution {
103103
* @return {number}
104104
*/
105105
var coinChange = function (coins, amount) {
106-
var dp = Array(amount + 1).fill(amount + 1);
107-
dp[0] = 0;
108-
for (var i = 1; i <= amount; i++) {
109-
for (var j = 0; j < coins.length; j++) {
110-
if (coins[j] <= i) {
111-
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
106+
let dp = Array(amount + 1).fill(amount + 1);
107+
dp[0] = 0;
108+
for (const coin of coins) {
109+
for (let j = coin; j <= amount; ++j) {
110+
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
111+
}
112+
}
113+
return dp[amount] > amount ? -1 : dp[amount];
114+
};
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
int coinChange(vector<int>& coins, int amount) {
123+
vector<int> dp(amount + 1, amount + 1);
124+
dp[0] = 0;
125+
for (auto coin : coins) {
126+
for (int j = coin; j <= amount; ++j) {
127+
dp[j] = min(dp[j], dp[j - coin] + 1);
112128
}
113129
}
130+
return dp[amount] > amount ? -1 : dp[amount];
114131
}
115-
116-
return dp[amount] > amount ? -1 : dp[amount];
117132
};
133+
```
118134
119135
<!-- tabs:end -->
120-
```
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
class Solution {
22
public:
33
int coinChange(vector<int>& coins, int amount) {
4-
std::vector<int> dp(amount + 1, -1);
4+
vector<int> dp(amount + 1, amount + 1);
55
dp[0] = 0;
6-
for (int i = 1; i <= amount; i++) {
7-
for (int j = 0; j < coins.size(); j++) {
8-
if (coins[j] <= i && dp[i - coins[j]] != -1) {
9-
// 当 当前值未被计算,或者有更小的组成方式的情况下
10-
if (dp[i] == -1 || dp[i] > dp[i - coins[j]] + 1) {
11-
dp[i] = dp[i - coins[j]] + 1;
12-
}
13-
}
6+
for (auto coin : coins) {
7+
for (int j = coin; j <= amount; ++j) {
8+
dp[j] = min(dp[j], dp[j - coin] + 1);
149
}
1510
}
16-
17-
return dp[amount];
11+
return dp[amount] > amount ? -1 : dp[amount];
1812
}
19-
};
13+
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ public int coinChange(int[] coins, int amount) {
1010
}
1111
return dp[amount] > amount ? -1 : dp[amount];
1212
}
13-
}
13+
}

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
* @return {number}
55
*/
66
var coinChange = function (coins, amount) {
7-
var dp = Array(amount + 1).fill(amount + 1);
7+
let dp = Array(amount + 1).fill(amount + 1);
88
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-
}
9+
for (const coin of coins) {
10+
for (let j = coin; j <= amount; ++j) {
11+
dp[j] = Math.min(dp[j], dp[j - coin] + 1);
1412
}
1513
}
16-
1714
return dp[amount] > amount ? -1 : dp[amount];
1815
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def coinChange(self, coins: List[int], amount: int) -> int:
3-
dp = [amount + 1 for i in range(amount + 1)]
3+
dp = [amount + 1] * (amount + 1)
44
dp[0] = 0
55
for coin in coins:
66
for j in range(coin, amount + 1):

solution/1400-1499/1486.XOR Operation in an Array/README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def xorOperation(self, n: int, start: int) -> int:
67+
res = 0
68+
for i in range(n):
69+
res ^= (start + (i << 1))
70+
return res
6671
```
6772

6873
### **Java**

solution/1400-1499/1486.XOR Operation in an Array/README_EN.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ Where &quot;^&quot; corresponds to bitwise XOR operator.
9797
### **Python3**
9898

9999
```python
100-
100+
class Solution:
101+
def xorOperation(self, n: int, start: int) -> int:
102+
res = 0
103+
for i in range(n):
104+
res ^= (start + (i << 1))
105+
return res
101106
```
102107

103108
### **Java**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def xorOperation(self, n: int, start: int) -> int:
3+
res = 0
4+
for i in range(n):
5+
res ^= (start + (i << 1))
6+
return res

0 commit comments

Comments
 (0)