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;
    }
};
相关推荐
无聊的小坏坏5 分钟前
一文讲通:二分查找的边界处理
数据结构·c++·算法
云深处@13 分钟前
【C++11】包装器,智能指针
开发语言·c++
十五年专注C++开发17 分钟前
CMake进阶:SelectLibraryConfigurations模块
c++·cmake·自动化构建
量子炒饭大师20 分钟前
【C++入门】Cyber深度漫游者的初始链路——【类与对象】初始化成员列表
开发语言·c++·dubbo·类与对象·初始化成员列表
TracyCoder12324 分钟前
LeetCode Hot100(50/100)——153. 寻找旋转排序数组中的最小值
算法·leetcode·职场和发展
mmz120731 分钟前
逆序对问题(c++)
c++·算法
化学在逃硬闯CS32 分钟前
Leetcode110.平衡二叉树
数据结构·c++·算法·leetcode
谢铭轩33 分钟前
题解:P8035 [COCI 2015/2016 #7] Otpor
c++·算法
爱coding的橙子41 分钟前
Day87:2.12:leetcode 动态规划8道题,用时3h
算法·leetcode·动态规划
阿猿收手吧!41 分钟前
【C++】模块:告别头文件新时代
开发语言·c++