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
}
相关推荐
geovindu2 分钟前
go: Singleton Pattern
单例模式·设计模式·golang
程序员爱德华6 分钟前
LeetCode刷题
算法·leetcode
memcpy07 分钟前
LeetCode 1202. 交换字符串中的元素【无向图连通分量】中等
算法·leetcode·职场和发展
Chase_______15 分钟前
LeetCode 643:子数组最大平均数 I
算法·leetcode
阿Y加油吧12 小时前
算法实战笔记:LeetCode 169 多数元素 & 75 颜色分类
笔记·算法·leetcode
不要秃头的小孩12 小时前
力扣刷题——509. 斐波那契数
python·算法·leetcode·动态规划
U盘失踪了12 小时前
Go 结构体
笔记·golang
We་ct13 小时前
LeetCode 120. 三角形最小路径和:动态规划详解
前端·javascript·算法·leetcode·typescript·动态规划
py有趣13 小时前
力扣热门100题之和为K的子数组
数据结构·算法·leetcode
py有趣15 小时前
力扣热门100题之编辑距离
数据结构·算法·leetcode