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
}
相关推荐
资深web全栈开发1 天前
LeetCode 1262. 可被三整除的最大和 - 解题思路与代码
算法·leetcode·职场和发展
资深web全栈开发1 天前
Golang Cobra 教程:构建强大的CLI应用
开发语言·后端·golang
JCGKS1 天前
Go| excelize的流式迭代器
后端·golang·excel·excelize·流式读取·文件解析
做怪小疯子1 天前
LeetCode 热题 100——链表——相交链表
算法·leetcode·链表
while(努力):进步1 天前
5G与物联网:连接万物的数字化未来
leetcode
2501_941804321 天前
C++在高性能互联网服务开发与系统优化中的应用与实战经验解析
leetcode
希望有朝一日能如愿以偿1 天前
力扣每日一题:可被三整除的最大和
数据结构·算法·leetcode
无敌最俊朗@1 天前
力扣hot100-环形链表(2)142
算法·leetcode·链表
Elias不吃糖1 天前
LeetCode每日一练(189, 122)
c++·算法·leetcode
小猪咪piggy1 天前
【算法】day 19 leetcode 100 矩阵+贪心
算法·leetcode·矩阵