Leetcode - 458 Poor Pigs
题目:
总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐?
分析:
为什么可怜不言而喻...本题可以这么考虑问题, 先是二维地排列罐子, 然后分别让两头猪去尝试找出哪一行和哪一列有毒.间隔时间为15分钟, 由于测试时间是60分钟 所以总共1只猪能测试5行或者5列. (这里不是4行或者4列, 因为如果前面4个测试猪都没死的话, 说明最后一行/最后一列肯定有毒). 总结一下,1个维度交给1只猪, 它鞠躬尽瘁死而后已, 能帮我们检查出(测试时间/毒发时间 + 1)个维度单位.
那么回到二维的例子里面, 2只猪能帮我们检查5*5=25个罐子,那么如果是三维, 就是5^3 = 125个, 以此类推随着维度的上升,只要最后的水桶数大于我们需要检查的水桶数,就需要几头猪.
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
代码:(相对简单)
public class solution{
public int poorPigs(int buckets, int timeToDie, int timeToTest) {
int pigs = 0;
while (Math.pow((timeToTest / timeToDie + 1), pigs) < buckets) {
pig++;
}
return pigs;
}
}
Leetcode - 458 Poor Pigs的更多相关文章
- 【LeetCode】458. Poor Pigs 解题报告(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
 - [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 ...
 - [LeetCode] 458. Poor Pigs_Easy tag: Math
		
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 ...
 - 1228.Poor Pigs 可怜的猪
		
转自[LeetCode] Poor Pigs 可怜的猪 There are 1000 buckets, one and only one of them contains poison, the re ...
 - 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 可怜的猪
		
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个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...
 - [Swift]LeetCode458. 可怜的小猪 | Poor Pigs
		
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...
 
随机推荐
- Go Revel - Parameters(参数绑定)
			
Go Revel - Parameters(参数绑定) 参数绑定 Revel框架会尽可能的将提交参数转换为期望的Go类型.这个从一个字符串提交参数转换为另一个类型被称为数据绑定 . 参数 所有的请求参 ...
 - 用django创建一个简单的sns
			
用django创建一个简单的sns 1.首先创建一个工程newsns django-admin.py startproject newsns 在工程目录下新建一个文件夹templates,在该文件夹下 ...
 - Http的四种post方式
			
1.引言 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PUT.DELETE.TRACE.CONNECT 这几种.其中 POST 一般用来向服务端提交 ...
 - Spring MVC---数据绑定和表单标签
			
数据绑定和表单标签 数据绑定 数据绑定是将用户输入绑定到领域模型的一种特性,在Spring MVC的controller和view数据传递 ...
 - C#通过外部别名,解决DLL冲突问题
			
今天遇到一个有两个DLL文件,命名空间,部分类名与部分方法名一样,但是方法的功能实现不一样.调用方法时,无法调用指定DLL的指定方法.在网上找了好多,简单总结一下. 1.首先添加引用,不细说. 2.右 ...
 - Hibernate3 第二天
			
Hibernate3 第二天 第一天回顾: 三个准备 创建数据库 准备po和hbm文件 准备灵魂文件hibernate.cfg.xml 七个步骤 1 加载配置文件Configuration 2 创建会 ...
 - Andrew Ng机器学习入门——线性回归
			
本人从2017年起,开始涉猎机器学习.作为入门,首先学习的是斯坦福大学Andrew Ng(吴恩达)教授的Coursera课程 2 单变量线性回归 线性回归属于监督学习(Supervise Learni ...
 - 利用fiddler将本地网页放到某个域下
			
注: 1)在学习慕课网课程<搜索框制作>中遇到如题困难,查找资料后解决,做此记录.课程网址http://www.imooc.com/video/263. 2)建议同时去学习慕课网课程< ...
 - URL传值问题,不同浏览器对URL的长度要求
			
通过URL传值的问题,所以对url字符串进行encodeURIComponent对url字符串内容进行编码,问题解决,但是有时候会出现 The request filtering module is ...
 - ListView控制消息
			
ListView控制消息 ListView控制消息是提供给父窗口或其他窗口通过发消息来控制ListView窗口本身. ListView控件提供给了以下消息来让外部程序控制自身: ListView_Ap ...