题目地址:Poor Pigs - LeetCode


There are 1000 buckets, one and only one of them is poisonous, while the rest are filled with water. They all look identical. If a pig drinks the poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket is poisonous within one hour?

Answer this question, and write an algorithm for the general case.

General case:

If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x) you need to figure out the poisonous bucket within p minutes? There is exactly one bucket with poison.

Note:

  • A pig can be allowed to drink simultaneously on as many buckets as one would like, and the feeding takes no time.
  • After a pig has instantly finished drinking buckets, there has to be a cool down time of m minutes. During this time, only observation is allowed and no feedings at all.
  • Any given bucket can be sampled an infinite number of times (by an unlimited number of pigs).

这道题目是一道数学智力题,具体解法参考这2篇文章:

  • 【数学】用信息论去解决「小白鼠试毒」问题
  • 1000桶水,其中一桶有毒,猪喝毒水后会在15分钟内死去,想用一个小时找到这桶毒水,至少需要几头猪?

Python解法如下:

class Solution:def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:states = minutesToTest // minutesToDie + 1return math.ceil(math.log(buckets) / math.log(states))

C++解法如下:

class Solution {public:int poorPigs(int buckets, int minutesToDie, int minutesToTest) {int states = minutesToTest / minutesToDie + 1;return ceil(log(buckets) / log(states)); }
};

LeetCode 458. Poor Pigs--智力题「小白鼠试毒」--C++,Python解法相关推荐

  1. 面试题——二进制相关(最小白鼠试毒问题)

    面试题--二进制相关(最小白鼠试毒问题) 题目 解题思路 总结

  2. 智力题------小白鼠试毒问题

    问题描述 有1000瓶水,其中有1瓶是有毒的.小白鼠喝了有毒的水之后24个小时就会死亡,问最少需要多少只小白鼠进行实验,才能在24小时内检测出哪瓶水有毒? 问题分析 如果没有时间限制的话,我们只要让一 ...

  3. 有趣问题——小白鼠试毒

    问:有1000瓶药水,其中只有一瓶有毒.现在用小白鼠进行实验,小白鼠只要服用任意量有毒药水就会在24小时内死亡.问至少要用多少只小白鼠进行实验才能检测出哪瓶药水有毒? 答: 给1000个瓶分别标上如下 ...

  4. 首批辉瑞疫苗紧急出仓,传特朗普将「以身试苗」

    点击上方"AI遇见机器学习",选择"星标"公众号 重磅干货,第一时间送达 来源:Bloomberg 编辑:小匀.QJB [导读]辉瑞公司首批新冠疫苗出仓,据称, ...

  5. DeepMind、哈佛造出了 AI「小白鼠」,从跑、跳、觅食、击球窥探神经网络的奥秘...

    机器之心报道 机器之心编辑部 我们可以用研究小白鼠的方式来研究 AI 吗?或许可以.在一篇 ICLR 2020 Spotlight 论文中,DeepMind 和哈佛大学的研究者构建了一个基于 AI 的 ...

  6. 这款NLP神器火了!关键词一键提取、结果高度可视化,堪称「小白进阶大神」的实用工具包 | 开源...

    点击上方,选择星标或置顶,不定期资源大放送! 阅读大概需要15分钟 Follow小博主,每天更新前沿干货 转载自:量子位 如何快速优雅地处理你的NLP数据集? 试试这款号称「从小白到大神」的Texth ...

  7. LeetCode 104. Maximum Depth of Binary Tree--二叉树高度--递归或迭代--C++,Python解法

    题目地址:Maximum Depth of Binary Tree - LeetCode Given a binary tree, find its maximum depth. The maximu ...

  8. LeetCode 137. Single Number II--三次异或消除相同的数--C++,Python解法

    题目地址:Single Number II - LeetCode Given a non-empty array of integers, every element appears three ti ...

  9. LeetCode 438. Find All Anagrams in a String--字符串-滑动窗口--C++,Python解法

    题目地址:Find All Anagrams in a String - LeetCode Given a string s and a non-empty string p, find all th ...

最新文章

  1. 滴滴CEO程维:当初把产品拿给美团王兴看,他说了两个字“垃圾”!
  2. fibonacci 数列及其应用
  3. Android数据存储
  4. CSS弹出二级多列菜单和DIV布局实例 - 仿IBM官网首页
  5. 大数据(11) - kafka的安装与使用
  6. CentOS 7.4创建普通用户赋予登录权限
  7. 有效的python属性管理:描述符的使用
  8. icem网格数和节点数_icem如何查看网格数量
  9. vb access mysql数据库教程_vb操作access数据库的方法
  10. 山水之道第四境——精灵的飞天之舞
  11. IC圈的世界杯 | 论芯片设计的胜利十一人
  12. 《沈剑架构师训练营》第1章 - 技术选型
  13. 数字IC设计的基本流程和主流EDA工具
  14. 端口复用|端口重映射
  15. 使用nid的修改数据库名
  16. 【厚积薄发系列】C++项目总结16—单例模式释放时机导致的崩溃问题分析
  17. RNA-seq工作流程:基因水平的探索性分析和差异表达
  18. 性能测试需求分析有哪些?怎么做?
  19. Zprotect1.4-1.6 patch KEY 脱壳
  20. Linux(Ubuntu16)下gedit自动补齐代码功能

热门文章

  1. RDKit | 基于RDKit(≥2020.09.1)的相似图绘制新方法
  2. Knowledge Graph |(1)图数据库Neo4j简介与入门
  3. C库函数-perror()
  4. mysql8.0 直方图_MySQL创建横向直方图
  5. python函数对变量的作用遵守原则有哪四条_创建一个包含四个参数的函数
  6. 免费申领Bio-protocol单细胞研究实验方法精选集
  7. 11月4日下午:植物保护高端论坛 (白洋、韦中)
  8. 公益合种油松专车3天领证
  9. 你想要的宏基因组-微生物组知识全在这(1802)
  10. R语言plotly可视化:plotly可视化分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半(Split violin plot in R with plotly)