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

题目:

题解:

cpp 复制代码
class Solution {
public:
    int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        if (buckets == 1) {
            return 0;
        }
        vector<vector<int>> combinations(buckets + 1,vector<int>(buckets + 1));
        combinations[0][0] = 1;
        int iterations = minutesToTest / minutesToDie;
        vector<vector<int>> f(buckets,vector<int>(iterations + 1));
        for (int i = 0; i < buckets; i++) {
            f[i][0] = 1;
        }
        for (int j = 0; j <= iterations; j++) {
            f[0][j] = 1;
        }
        for (int i = 1; i < buckets; i++) {
            combinations[i][0] = 1;
            combinations[i][i] = 1;
            for (int j = 1; j < i; j++) {
                combinations[i][j] = combinations[i - 1][j - 1] + combinations[i - 1][j];
            }
            for (int j = 1; j <= iterations; j++) {
                for (int 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;
    }
};
相关推荐
DARLING Zero two♡5 分钟前
C++内存列传之RAII宇宙:智能指针
c++·c++11·智能指针
阿飞__31 分钟前
C++ 使用 ffmpeg 解码本地视频并获取每帧的YUV数据
c++·ffmpeg·音视频
鑫鑫向栄36 分钟前
[蓝桥杯]最优包含
数据结构·c++·算法·职场和发展·蓝桥杯·深度优先
泛舟起晶浪38 分钟前
网络寻路--图论
c++·算法
YKPG1 小时前
C++学习-入门到精通【13】标准库的容器和迭代器
c++·学习·stl
hn小菜鸡1 小时前
LeetCode 3370.仅含置位位的最小整数
算法·leetcode
早日退休!!!1 小时前
C++性能优化指南
开发语言·c++·性能优化
whoarethenext5 小时前
使用 C++/OpenCV 图像直方图比较两个图片相似度
开发语言·c++·opencv·直方图·相似度对比
花自向阳开10248 小时前
LeetCode hot100-11
数据结构·算法·leetcode
月亮被咬碎成星星8 小时前
LeetCode[404]左叶子之和
算法·leetcode