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
}
相关推荐
microrain2 小时前
跟着一条属性报文走一遍 SagooIoT 的接入链路
物联网·架构·golang·sagooiot
CodeGL2 小时前
微服务框架——go-zero
微服务·架构·golang
Navigator_Z2 小时前
LeetCode //C - 1311. Get Watched Videos by Your Friends
c语言·算法·leetcode
Pniubi3 小时前
力扣42接雨水(动态规划法)
算法·leetcode·职场和发展
Pniubi4 小时前
力扣46全排列(回溯(递归))
算法·leetcode·职场和发展
文丑颜不良啊4 小时前
LeetCode1461~1480题目解析
java·leetcode
Pniubi14 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
旖旎夜光1 天前
LeetCode 315: 算右侧小于当前元素的个数(归并排序) —— 题解
c++·学习·算法·leetcode·排序算法
文丑颜不良啊1 天前
LeetCode1401~1420题目解析
java·leetcode
Nuanyt1 天前
LeetCode Hot 100 链表专题2 3 题 (Java)
java·笔记·算法·leetcode·链表