1228.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.
猪只能喝一次
假设只有1只猪,只有15分钟,那么我们能测几个水桶呢?很显然是两个,因为只能测一次的话,让猪去随便喝一桶,如果毒死了,就是喝的那桶,反之则是另一桶。
如果有两只猪呢,能测几桶?怎么喝呢,两只猪一猪喝一桶,再同时喝一桶,剩下一桶谁也不喝,那么如果两只猪都毒死了,说明是共同喝的那桶有毒,如果某个猪毒死了,说明该猪喝的那桶有毒,如果都没事,说明是谁也没喝的那桶。
三猪能测8桶,其实就是2的指数倍。第一只猪喝1、4、5、7桶,第二只2、4、6、7,第三只3、5、6、7。即\(1+C_3^1+C_3^2+C_3^3+1=2^3\)
如果只能测一次的话,实际上相当一个一维数组,而如果能测两次的话,情况就不一样了,我们就可以重复利用猪了。
比如还是两只猪,能测两次,功能测几个桶,答案可以测9桶,为啥,我们组个二维数组:
1 2 3
4 5 6
7 8 9
如果我们让第一头猪第一次喝1,2,3桶,第二次喝4,5,6桶,而让第二头猪第一次喝1,4,7桶,第二次喝2,5,8桶,我们可以根据猪的死亡情况来确定是哪一桶的问题,实际上就把猪被毒死的那个节点当作了二维数组的横纵坐标来定位毒桶的位置,巧妙吧~更巧妙的是,如果再增加一头猪,实际上是给数组增加一个维度,变成了一个三维数组,那么三只猪,测两次,可以测27桶,叼不叼。这道题让我们求最少用多少猪来测,那么就是求数组的维度,我们知道了数组的总个数,所以要尽量增加数组的长宽,尽量减少维度。这里,数组的长宽其实都是测试的次数+1,所以我们首先要确定能测的次数,通过总测试时间除以毒发时间,再加上1就是测试次数。有了数组长宽m,那么如果有x只猪,能测的桶数为m的x次方,现在我们给定了桶数N,要求x,就log一下就行,然后用个换底公式,就可以求出x的值了,参见代码如下:
class Solution {
public:
int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
return ceil(log(buckets) / log(minutesToTest / minutesToDie + 1));
}
};
1228.Poor Pigs 可怜的猪的更多相关文章
- [LeetCode] Poor Pigs 可怜的猪
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- Leetcode458.Poor Pigs可怜的小猪
有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...
- Leetcode - 458 Poor Pigs
题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...
- [Swift]LeetCode458. 可怜的小猪 | Poor Pigs
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- 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 ...
- 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(Java实现)
这是悦乐书的第235次更新,第248篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第102题(顺位题号是455).有1000个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...
- [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 ...
随机推荐
- 模拟placeholder
把这个记下来,主要是因为这里的 defaultValue 我之前竟然不知道 <input type="text" value="提示内容" onFocus ...
- MyBatis知识点总结(一)
前言:本篇主要记录在MyBatis学习过程中的主要知识点. 1.mybatis环境的搭建,通过maven可以快速的进行环境的搭建. <!--文件版本--> <properties&g ...
- 设计模式のProxyPattern(代理模式)----结构模式
一.产生背景 在直接访问对象时带来的问题,比如说:要访问的对象在远程的机器上.在面向对象系统中,有些对象由于某些原因(比如对象创建开销很大,或者某些操作需要安全控制,或者需要进程外的访问),直接访问会 ...
- 兼容Android 和 ios JavaScript copy paste
<!DOCTYPE html> <html> <head> <title>关于我们Frame</title> <meta charse ...
- 002_cookie的session_id解释
HTTP协议(http://www.w3.org/Protocols/)是“一次性单向”协议. 服务端不能主动连接客户端,只能被动等待并答复客户端请求.客户端连接服务端,发出一个HTTP Reques ...
- Access restriction: The type 'BASE64Encoder'
Access restriction: The type 'BASE64Encoder' is not API (restriction on required library 'D:\Java\jd ...
- Spring Security(十七):5.8 Method Security
From version 2.0 onwards Spring Security has improved support substantially for adding security to y ...
- 破解windows版idea
近期使用idea的时候提示已过试用期,需要购买,否则只能使用半小时 网上破解方法很全,记录一下 1.下载破解补丁 百度网盘:https://pan.baidu.com/s/15Wq9-c4SVwg_2 ...
- vue是一个渐进式的框架,如何理解“渐进式”
每个框架都不可避免会有自己的一些特点,从而会对使用者有一定的要求,这些要求就是主张,主张有强有弱,它的强势程度会影响在业务开发中的使用方式.使用vue,你可以在原有大系统的上面,把一两个组件改用它实现 ...
- mysql 索引原理
一.索引的本质 MySQL官方对索引的定义为:索引(Index)是帮助MySQL高效获取数据的数据结构.提取句子主干,就可以得到索引的本质:索引是数据结构. 我们知道,数据库查询是数据库的最主要功能之 ...