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.

My binary encoding method gives a 8 for the first problem, however, there is a smarter method that generates a answer of 5, which can be found

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

With 2 pigs, poison killing in 15 minutes, and having 60 minutes, we can find the poison in up to 25 buckets in the following way. Arrange the buckets in a 5×5 square:

 1  2  3  4  5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

Now use one pig to find the row (make it drink from buckets 1, 2, 3, 4, 5, wait 15 minutes, make it drink from buckets 6, 7, 8, 9, 10, wait 15 minutes, etc). Use the second pig to find the column.

Having 60 minutes and tests taking 15 minutes means we can run four tests. If the row pig dies in the third test, the poison is in the third row. If the column pig doesn't die at all, the poison is in the fifth column (this is why we can cover five rows/columns even though we can only run four tests).

With 3 pigs, we can similarly use a 5×5×5 cube instead of a 5×5 square and again use one pig to determine the coordinate of one dimension. So 3 pigs can solve up to 125 buckets.

In general, we can solve up to (⌊minutesToTest / minutesToDie⌋ + 1)^pigs buckets this way

 public class Solution {
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
int pigs = 0;
int tests = minutesToTest / minutesToDie + 1;
while (Math.pow(tests, pigs) < buckets) {
pigs++;
}
return pigs;
}
}

Leetcode: Poor Pigs的更多相关文章

  1. [LeetCode] Poor Pigs 可怜的猪

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

  2. 1228.Poor Pigs 可怜的猪

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

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

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

  4. Leetcode - 458 Poor Pigs

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

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

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

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

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

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

  8. 458. Poor Pigs

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

  9. Leetcode458.Poor Pigs可怜的小猪

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

随机推荐

  1. JAVA中List 排序

    第一种方法,就是list中对象实现Comparable接口,代码如下: public class SortEntity implements Comparable<SortEntity>{ ...

  2. PHP 汉字数字互转(100以内)| PHP 汉字转数字 | PHP数字转汉字

    <?php function numDatabase(){ $numarr =array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,2 ...

  3. SpringBoot配置属性之DataSource

    https://segmentfault.com/a/1190000004316491

  4. Qt里的slot

    昨天出了一个小bug, 一直调都没调出来, 今天仔细看了下, 发现出错的原因了. 我在用osgEarth的时候, 用到一个类MapCatalogWidget, 觉得它不够用, 就把这个类给改了下, 添 ...

  5. ArrayList实现删除重复元素(元素不是对象类型的情况)

    package 集合; import java.util.ArrayList;import java.util.Iterator; /* * 去除ArrayList里面的重复元素 *  * */pub ...

  6. Javascript初学篇章_4(循环与函数)

    七.循环语句 1.While 语法: while (exp){ //statements; } 说明: while (变量<=结束值){ 需执行的代码 } 例: var i=0; while(i ...

  7. NeoKylin5.6下安装部署达梦(DM7)数据库

    1.准备操作系统 1.1 系统登录界面 1.2 操作系统版本信息 [root@jdbh ~]# uname -ra Linux jdbh -.el5xen # SMP Fri Jul :: EDT x ...

  8. Tomcat启动后,从spring容器中获取Bean和ServletContext

    public static Object getBean(String beanName){ ApplicationContext context = ContextLoader.getCurrent ...

  9. jQuery插件开发(转)

    jQuery插件开发全解析 jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命 ...

  10. POST在发送数据的时候使用的是HTTP命令

    防止SQL注入 SQL(结构化查询语言)是基于美国国家标准学会(ANSI)标准,并作为共同的语言与数据库通信.每个数据库系统增加了一些专有功能到基本的ANSI SQL. SQL注入是一门将制作好的SQ ...