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;
    }
};
相关推荐
carver w1 小时前
c++ 容器vector基础
开发语言·c++
小陈又菜3 小时前
【C++】类和对象--类中6个默认成员函数(2) --运算符重载
开发语言·c++·运算符重载
夜斗小神社4 小时前
【LeetCode 热题 100】(六)矩阵
算法·leetcode·矩阵
weixin_307779136 小时前
C++实现MATLAB矩阵计算程序
开发语言·c++·算法·matlab·矩阵
捏尼卜波卜6 小时前
try/catch/throw 简明指南
linux·开发语言·c++
_poplar_6 小时前
09 【C++ 初阶】C/C++内存管理
c语言·开发语言·数据结构·c++·git·算法·stl
GetcharZp7 小时前
C++ Boost 从入门到精通:让你的代码飞起来
c++·后端
mit6.8247 小时前
[LVGL] 配置lv_conf.h | 条件编译 | 显示屏lv_display
c++·mfc
板鸭〈小号〉8 小时前
线程安全的单例模式,STL和智能指针
开发语言·c++·单例模式
阿飞__8 小时前
C++使用FFmpeg进行视频推流
c++·ffmpeg·音视频