Leetcode: 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.
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的更多相关文章
- [LeetCode] Poor Pigs 可怜的猪
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
- 1228.Poor Pigs 可怜的猪
转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...
- 【LeetCode】458. Poor Pigs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Leetcode - 458 Poor Pigs
题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...
- 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 ...
- [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 ...
- Leetcode458.Poor Pigs可怜的小猪
有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...
随机推荐
- Android-组件RadioButton使用技巧
当初第一次接触Android组件的时候,我感觉微信底部的菜单,是用button这样的组件来做的. 可我想错了.却是用RadioButton来做的.那到底怎么做,接下来我就做一下分享!希望看了之后你也觉 ...
- jQuery插件(cookie存值)
使用cookie插件后,可以很方便地通过cookie对象保存.读取.删除用户的信息,还能通过cookie插件保存用户的浏览记录,它的调用格式为: 保存:$.cookie(key,value):读取:$ ...
- activity 、window与view的关系(下)
在activity的attacth方法中,通过policymanager 的makenewwindow来创建window 而window的具体实现是phonewindow 接下来通过setconten ...
- [代码]label增加删除线
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 100, 30)]; [self.view addSubvi ...
- Git 常用命令行
最近在公司的服务器上安装了Git Sever,开始从SVN转向到Git了,整理了一些在Git常用的命令. 取得Git仓库 初始化一个版本仓库 git initClone远程版本库 git clone ...
- CentOS7 编译安装 Mariadb (实测 笔记 Centos 7.0 + Mariadb 10.0.15)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...
- 03.深入javascript
函数返回值 函数返回值和函数传参正好相反,函数传参是我们可以把一些东西传到函数里面去,函数返回值是函数可以把一些东西传到外面来. <script> function show() { re ...
- bzoj1745: [Usaco2005 oct]Flying Right 飞行航班(贪心+map)
之前做过一道基本一样的题目,抽象出来就是有个容量为c的载体,一些线段上某个点到另一个点要运输w个东西,求从头到尾最多能运多少东西. 这种模型可以用贪心做,用map,map[r]表示r的那个点,我们准备 ...
- 2016huasacm暑假集训训练四 递推_A
题目链接:http://acm.hust.edu.cn/vjudge/contest/125308#problem/A 这题主要考的就是就是一个排列公式,但是不能用阶乘的公式, 用这个公式不易超 ...
- Struts2_ValueStack,OGNL详解(转)
原文地址:http://blog.csdn.net/wyply115/article/details/8257140 一.OGNL表达式 1.ognl是struts2中使用的一种表达式语言,可用于js ...