本文主要是介绍Golang | Leetcode Golang题解之第375题猜数字大小II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
func getMoneyAmount(n int) int {f := make([][]int, n+1)for i := range f {f[i] = make([]int, n+1)}for i := n - 1; i >= 1; i-- {for j := i + 1; j <= n; j++ {f[i][j] = j + f[i][j-1]for k := i; k < j; k++ {cost := k + max(f[i][k-1], f[k+1][j])if cost < f[i][j] {f[i][j] = cost}}}}return f[1][n]
}func max(a, b int) int {if b > a {return b}return a
}
这篇关于Golang | Leetcode Golang题解之第375题猜数字大小II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!