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
}
相关推荐
tkevinjd4 分钟前
力扣148-排序链表
算法·leetcode·链表
web守墓人41 分钟前
【go语言】gotar:使用go语言复刻tar命令,并加入7z支持,可独立运行在windows、linux、macos上
linux·macos·golang
看-是灰机1 小时前
使用go语言实现对接
linux·开发语言·后端·docker·语言模型·golang·飞书
名字还没想好☜15 小时前
Go 的 time.Ticker 陷阱:定时任务里被忽略的内存泄漏与正确关闭
java·数据库·golang·go·定时器
Navigator_Z20 小时前
LeetCode //C - 1156. Swap For Longest Repeated Character Substring
c语言·算法·leetcode
灯澜忆梦1 天前
Go 语言 _JSON---序列化与反序列化
开发语言·golang·json
布朗克1681 天前
Go入门到精通-22-同步原语
开发语言·后端·golang·同步原语
兰令水1 天前
hot100【acm版】【2026.7.19打卡-java版本】
java·数据结构·算法·leetcode·面试
怒放de生命20101 天前
【web3基础】go-zero使用etcd实现服务注册与发现(四)
golang·web3·etcd·go-zero
灯澜忆梦1 天前
Go语言_文件---文件操作
开发语言·后端·golang