Golang | Leetcode Golang题解之第458题可怜的小猪

题目:

题解:

Go 复制代码
func poorPigs(buckets, minutesToDie, minutesToTest int) int {
    if buckets == 1 {
        return 0
    }

    combinations := make([][]int, buckets+1)
    for i := range combinations {
        combinations[i] = make([]int, buckets+1)
    }
    combinations[0][0] = 1

    iterations := minutesToTest / minutesToDie
    f := make([][]int, buckets)
    for i := range f {
        f[i] = make([]int, iterations+1)
    }
    for i := 0; i < buckets; i++ {
        f[i][0] = 1
    }
    for j := 0; j <= iterations; j++ {
        f[0][j] = 1
    }

    for i := 1; i < buckets; i++ {
        combinations[i][0] = 1
        for j := 1; j < i; j++ {
            combinations[i][j] = combinations[i-1][j-1] + combinations[i-1][j]
        }
        combinations[i][i] = 1
        for j := 1; j <= iterations; j++ {
            for k := 0; k <= i; k++ {
                f[i][j] += f[k][j-1] * combinations[i][i-k]
            }
        }
        if f[i][iterations] >= buckets {
            return i
        }
    }
    return 0
}
相关推荐
.道阻且长.8 小时前
5.LeetCode算法习题讲解--双指针--有效三角形的个数
算法·leetcode·职场和发展
名字还没想好☜12 小时前
Go 的 unsafe.Pointer 实战:零拷贝 []byte↔string 转换与三条铁律
开发语言·后端·golang·go·unsafe
xin_nai12 小时前
LeetCode热题100(Java)(7)链表(下)
java·leetcode·链表
想吃火锅100512 小时前
【leetcode】54. 螺旋矩阵
算法·leetcode·矩阵
想吃火锅100512 小时前
【leetcode】300. 最长递增子序列
算法·leetcode·职场和发展
xzlAwin13 小时前
Go语言多版本管理器g工具命令
开发语言·golang
songtaiwu15 小时前
Go泛型的应用
开发语言·后端·golang
Hi李耶16 小时前
【LeetCode】15.三数之和
java·算法·leetcode
rannn_11117 小时前
【力扣hot100】链表专题|21、2、19、24、92、25
java·数据结构·算法·leetcode·链表·开发
鹿角片ljp18 小时前
LeetCode 21. 合并两个有序链表
算法·leetcode·链表