Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix][golang] coin-change #1571

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 38 additions & 42 deletions 多语言解法代码/solution_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -12934,52 +12934,48 @@ class Solution {
```

```go
// by chatGPT (go)
// by mario_huang (go)
var memo []int

func coinChange(coins []int, amount int) int {
memo := make([]int, amount+1)
for i := 0; i <= amount; i++ {
memo[i] = -666
}
return dp(coins, amount, memo)
memo = make([]int, amount+1)
// 备忘录初始化为一个不会被取到的特殊值,代表还未被计算
for i := 0; i <= amount; i++ {
memo[i] = -666
}
return dp(coins, amount)
}

func dp(coins []int, amount int, memo []int) int {
if amount == 0 {
return 0
}
if amount < 0 {
return -1
}
// 查备忘录,防止重复计算
if memo[amount] != -666 {
return memo[amount]
}

res := math.MaxInt32
for _, coin := range coins {
// 计算子问题的结果
subProblem := dp(coins, amount-coin, memo)
// 子问题无解则跳过
if subProblem == -1 {
continue
}
// 在子问题中选择最优解,然后加一
res = min(res, subProblem+1)
}
// 把计算结果存入备忘录
if res == math.MaxInt32 {
memo[amount] = -1
} else {
memo[amount] = res
}
return memo[amount]
}
func dp(coins []int, amount int) int {
if amount == 0 {
return 0
}
if amount < 0 {
return -1
}
// 查备忘录,防止重复计算
if memo[amount] != -666 {
return memo[amount]
}

func min(a, b int) int {
if a < b {
return a
}
return b
res := math.MaxInt32
for _, coin := range coins {
// 计算子问题的结果
subProblem := dp(coins, amount-coin)
// 子问题无解则跳过
if subProblem == -1 {
continue
}
// 在子问题中选择最优解,然后加一
res = min(res, subProblem+1)
}
// 把计算结果存入备忘录
if res == math.MaxInt32 {
memo[amount] = -1
} else {
memo[amount] = res
}
return memo[amount]
}
```

Expand Down