Skip to content

Commit 9f0e89f

Browse files
committedJul 24, 2021
feat: add golang solution to lc problem: No.0322. Coin Change
1 parent 7e1f274 commit 9f0e89f

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
- [摆动序列](./solution/0300-0399/0376.Wiggle%20Subsequence/README.md)
199199
- [最长公共子序列](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
200200
- [编辑距离](./solution/0000-0099/0072.Edit%20Distance/README.md)
201+
- [零钱兑换](./solution/0300-0399/0322.Coin%20Change/README.md)
201202
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
202203
- [俄罗斯套娃信封问题](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)
203204

‎README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
192192
- [Wiggle Subsequence](./solution/0300-0399/0376.Wiggle%20Subsequence/README_EN.md)
193193
- [Longest Common Subsequence](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md)
194194
- [Edit Distance](./solution/0000-0099/0072.Edit%20Distance/README_EN.md)
195+
- [Coin Change](./solution/0300-0399/0322.Coin%20Change/README_EN.md)
195196
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
196197

197198
### Backtracking

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

+29
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161

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

64+
动态规划。
65+
6466
类似完全背包的思路,硬币数量不限,求凑成总金额所需的最少的硬币个数。
6567

6668
<!-- tabs:start -->
@@ -138,4 +140,31 @@ public:
138140
};
139141
```
140142
143+
### **Go**
144+
145+
```go
146+
func coinChange(coins []int, amount int) int {
147+
dp := make([]int, amount+1)
148+
for i := 1; i <= amount; i++ {
149+
dp[i] = amount + 1
150+
}
151+
for _, coin := range coins {
152+
for j := coin; j <= amount; j++ {
153+
dp[j] = min(dp[j], dp[j-coin]+1)
154+
}
155+
}
156+
if dp[amount] > amount {
157+
return -1
158+
}
159+
return dp[amount]
160+
}
161+
162+
func min(a, b int) int {
163+
if a < b {
164+
return a
165+
}
166+
return b
167+
}
168+
```
169+
141170
<!-- tabs:end -->

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

+29
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959

6060
## Solutions
6161

62+
Dynamic programming.
63+
6264
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.
6365

6466
<!-- tabs:start -->
@@ -132,4 +134,31 @@ public:
132134
};
133135
```
134136
137+
### **Go**
138+
139+
```go
140+
func coinChange(coins []int, amount int) int {
141+
dp := make([]int, amount+1)
142+
for i := 1; i <= amount; i++ {
143+
dp[i] = amount + 1
144+
}
145+
for _, coin := range coins {
146+
for j := coin; j <= amount; j++ {
147+
dp[j] = min(dp[j], dp[j-coin]+1)
148+
}
149+
}
150+
if dp[amount] > amount {
151+
return -1
152+
}
153+
return dp[amount]
154+
}
155+
156+
func min(a, b int) int {
157+
if a < b {
158+
return a
159+
}
160+
return b
161+
}
162+
```
163+
135164
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func coinChange(coins []int, amount int) int {
2+
dp := make([]int, amount+1)
3+
for i := 1; i <= amount; i++ {
4+
dp[i] = amount + 1
5+
}
6+
for _, coin := range coins {
7+
for j := coin; j <= amount; j++ {
8+
dp[j] = min(dp[j], dp[j-coin]+1)
9+
}
10+
}
11+
if dp[amount] > amount {
12+
return -1
13+
}
14+
return dp[amount]
15+
}
16+
17+
func min(a, b int) int {
18+
if a < b {
19+
return a
20+
}
21+
return b
22+
}

0 commit comments

Comments
 (0)
Please sign in to comment.