coin_change
coin_change.go 源码
package main
import "math"
//零钱兑换
func coinChange(coins []int, amount int) int {
dp := make([]int, amount+1)
for i := range dp {
dp[i] = amount + 1
}
dp[0] = 0
for i := 1; i <= amount; i++ {
for _, c := range coins {
if i >= c {
dp[i] = int(math.Min(float64(dp[i]), float64(dp[i-c]+1)))
}
}
}
if dp[amount] > amount {
return -1
}
return dp[amount]
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦