458. Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket contains the poison within one hour.
Answer this question, and write an algorithm for the follow-up general case.
Follow-up:
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 "poison" bucket within p minutes? There is exact one bucket with poison.
N个桶,一只猪喝了会在minutesToDie分钟内死亡,你有minutesToTest分钟的时间,求最少要多少猪试出毒药
如果有4个桶,15分钟死亡,有15分钟的时间,用二进制对桶编号
00 01 10 11
__ _A B_ BA
0表示没喝
1表示喝了
_表示没喝 如果A挂了,B没挂,则是01桶 最少要2只猪
如果有8个桶,15分钟死亡,有30分钟的时间,用3进制对桶编号,并且有2轮测试

0表示两轮都不喝
1表示第一轮喝,第二轮不喝
2表示第一轮不喝,第二轮喝
最后推出公式为(测试次数+1)^x >= 桶数 求x的最小整数值
C++(2ms):
class Solution {
public:
int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
return ceil(log(buckets)/log(minutesToTest/minutesToDie +)) ;
}
};
458. Poor Pigs的更多相关文章
- Leetcode - 458 Poor Pigs
题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...
- [LeetCode&Python] Problem 458. Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- 【LeetCode】458. Poor Pigs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 1228.Poor Pigs 可怜的猪
转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...
- Leetcode: Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- [LeetCode] Poor Pigs 可怜的猪
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- [Swift]LeetCode458. 可怜的小猪 | Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- [LeetCode] 458. Poor Pigs_Easy tag: Math
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- LeetCode算法题-Poor Pigs(Java实现)
这是悦乐书的第235次更新,第248篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第102题(顺位题号是455).有1000个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...
随机推荐
- vue添加属性绑定
html <div id="app-2"> <span v-bind:title="message"> 鼠标悬停几秒钟查看此处动态绑定的 ...
- Windows10实用技巧-固定快捷方式到磁贴菜单方式
快捷方式固定到磁贴 Win10的开始菜单中的磁贴功能比较不错,可以在不清理桌面上其他软件的情况下直接唤醒需要的应用. 但是比较麻烦的是一些应用或快捷方式并不能直接固定到上面. 后来发现所有Windo ...
- Windows下的包管理器Chocolatey
参考文档: https://www.jianshu.com/p/831aa4a280e7 https://www.jianshu.com/p/abaa0e8c261f
- 使用L2正则化和平均滑动模型的LeNet-5MNIST手写数字识别模型
使用L2正则化和平均滑动模型的LeNet-5MNIST手写数字识别模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献Tensorflow实战Google深度学习框架 实验平台: T ...
- [Wc2007]剪刀石头布
[Wc2007]剪刀石头布 http://www.lydsy.com/JudgeOnline/problem.php?id=2597 Time Limit: 20 Sec Memory Limit: ...
- Flexbox兼容性
.flex-container{ display: -webkit-box; /* Chrome 4+, Safari 3.1, iOS Safari 3.2+ */ display: -moz-bo ...
- 2D旋转和3D旋转
2D旋转 先给个容器 <p onClick="rotate2D()" id="rotate2D" class="animated_div&quo ...
- JavaScript-变量与作用域链
jQuery片段: 1 var 2 // Will speed up references to window, and allows munging its name. 3 win ...
- 20155117 王震宇 2006-2007-2 《Java程序设计》第三周学习总结
20155117 王震宇 2006-2007-2 <Java程序设计>第三周学习总结 教材学习内容总结 在JAVA程序编写中,常常要用到对象(Object),要产生对象首先要定义类(Cla ...
- 认识单点登录cas
么是单点登录?单点登录全称Single Sign On(以下简称SSO),是指在多系统应用群中登录一个系统,便可在其他所有系统中得到授权而无需再次登录,包括单点登录与单点注销两部分 1.登录 相比于单 ...