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.

这道题博主拿到以后并木有什么头绪,可是明明标的是Easy,打击甚大,于是去论坛上大神们的解法,感觉这道题应该算是一道Brain Teaser的题,对问题的分析能力要求很高。那么我们来一步一步从最简单的情况来分析吧,假设只有1只猪,只有15分钟,那么我们能测几个水桶呢?很显然是两个,因为只能测一次的话,让猪去随便喝一桶,如果毒死了,就是喝的那桶,反之则是另一桶。好,那么如果有两只猪呢,能测几桶?怎么喝呢,两只猪一猪喝一桶,再同时喝一桶,剩下一桶谁也不喝,那么如果两只猪都毒死了,说明是共同喝的那桶有毒,如果某个猪毒死了,说明该猪喝的那桶有毒,如果都没事,说明是谁也没喝的那桶。那么我们应该看出规律了吧,没错,三猪能测8桶,其实就是2的指数倍。

如果只能测一次的话,实际上相当一个一维数组,而如果能测两次的话,情况就不一样了,我们就可以重复利用猪了。比如还是两只猪,能测两次,功能测几个桶,答案可以测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 + ));
}
};

参考资料:

https://discuss.leetcode.com/topic/67666/another-explanation-and-solution

https://discuss.leetcode.com/topic/67482/solution-with-detailed-explanation

https://discuss.leetcode.com/topic/70733/1-line-solution-with-detailed-problem-clarification-math-proof-please-read-if-you-really-want-to-know-what-this-problem-means

[LeetCode] Poor Pigs 可怜的猪的更多相关文章

  1. 1228.Poor Pigs 可怜的猪

    转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...

  2. Leetcode: Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  3. Leetcode458.Poor Pigs可怜的小猪

    有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...

  4. Leetcode - 458 Poor Pigs

    题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...

  5. [Swift]LeetCode458. 可怜的小猪 | Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  6. 【LeetCode】458. Poor Pigs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. LeetCode算法题-Poor Pigs(Java实现)

    这是悦乐书的第235次更新,第248篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第102题(顺位题号是455).有1000个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...

  8. [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 ...

  9. 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

随机推荐

  1. C语言第一次作业——输入输出格式

    题目1温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代码 #include& ...

  2. 基于Python的Web应用开发实践总结

    基于Python的Web应用开发学习总结 项目地址   本次学习采用的是Flask框架.根据教程开发个人博客系统.博客界面如图所示. 整个学习过程收获很多,以下是学习总结. 1.virtualenv ...

  3. listview、gradview滚动到最后时,滑动至顶部

    listview.gradview滑动顶端.底部的判断及底部滑动至顶端 mPhotoWall.setOnScrollListener(new AbsListView.OnScrollListener( ...

  4. 关于第一次使用vue-cli

    前段时间终于终于可以用vue-cli,webpack做个企业站,记一下过程... 首先node.js,按照vue官网的步骤命令提示符走一波,网速原因,所以用的是淘宝镜像 cnpm # 全局安装 vue ...

  5. js判断操作系统windows,ios,android(笔记)

    使用JS判断用户使用的系统是利用浏览器的userAgent. navigator.userAgent:userAgent 获取了浏览器用于 HTTP 请求的用户代理头的值. navigator.pla ...

  6. 关于PHP7

    目前一直使用php7也看了许多文档视频等,整理一下相关细节(仅为记录-),对于PHP7性能,如下图所示. * 在wordpress3.0.1中 php7比php5.6性能提升约3倍左右 新特性 一.变 ...

  7. Python内置函数(55)——globals

    英文文档: globals() Return a dictionary representing the current global symbol table. This is always the ...

  8. Python内置函数(5)——pow

    英文文档: pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (co ...

  9. 零基础大数据入门教程:Java调用阿里云短信通道服务

    这里我们使用SpringBoot 来调用阿里通信的服务. 阿里通信,双11.收到短信,日发送达6亿条.保障力度非常高. 使用的步骤: 1.1. 第一步:需要开通账户 1.2. 第二步:阅读接口文档 1 ...

  10. SpringBoot应用的前台目录

    一.两个重要目录 templates:存放web页面的模板文件,需要在controller返回视图名称,框架转发才能找到的html. static :存放静态资源,如:html(放在这里可直接访问,如 ...