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;
}
}

最新文章

  1. Windows Media Player安装了却不能播放网页上的视频
  2. Tkinter教程之Canvas篇(3)
  3. SQL Server自动化运维系列 - 多服务器数据收集和性能监控
  4. linux下无ifconfig命令
  5. Ajax实现在textbox中输入内容,动态从数据库中模糊查询显示到下拉框中
  6. UTF-8和UTF-8无BOM,一个会导致文件中中文变量无法匹配的bug
  7. JAVA9模块化详解(二)——模块的使用
  8. JAVA_SE基础——47.接口
  9. 使用伪类before和after
  10. mysql高级、索引
  11. 哪些个在 Sublime Text 下,&quot;任性的&quot; 好插件!
  12. 解决nginx转发websocket报400错误
  13. swift 基础小结01 --delegate、Optional、GCD的使用、request请求、网络加载图片并保存到沙箱、闭包以及桥接
  14. STM32f103的数电采集电路的双ADC的设计与使用
  15. -bash: brew: command not found
  16. c++builder 代码模板 code templates
  17. iOS开发之地域选择
  18. poj3974 Palindrome【回文】【Hash】【二分】
  19. lua垃圾回收之空表
  20. kafka log文件和offset原理

热门文章

  1. 关于datatime 时间处理模块:
  2. hdu 5677 ztr loves substring 多重背包
  3. C# 词法分析器(五)转换 DFA
  4. 《Invert》开发日志04:工具、资源和服务
  5. 惠普披甲过VR寒冬,花费巨资开发VR游戏
  6. React Native开发之npm start加速
  7. [DEMO] 互联网广告RTB机制简介
  8. [IOS]译Size Classes with Xcode 6: One Storyboard for all Sizes
  9. 使用XML文件记录操作日志,并从后往前读取操作日志并在richTextBox1控件中显示出来
  10. DoModal时带出次级窗口闪现