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 ...
随机推荐
- MYSQL 分表实践
基本条件: 无索引 主表 test_0 数据:一百万条 数据库引擎 InnoDb 分表 test_1...test_100 数据 每张一万条,一共一百万条 数据库引擎 InnoDb 流程: 主表中 ...
- PyQt设计流程
Qt designer 设计流程: 创建一个 PyQt4 的步骤,通常的方法是用 QtDesigner 工具创建 GUI 界面.可以在窗口 上添加部件,并可以对部件进行一些属性配置.一般的过程如下 ...
- CI持续集成
CI持续集成 “我的TDD实践”系列之CI持续集成 写在前面: 我的TDD实践这几篇文章主要是围绕测试驱动开发所展开的,其中涵盖了一小部分测试理论,更多的则是关注工具的使用及环境的搭建,做到简单实践先 ...
- enode框架step by step之Staged event-driven architecture思想的运用
enode框架step by step之Staged event-driven architecture思想的运用 enode框架系列step by step文章系列索引: 分享一个基于DDD以及事件 ...
- Ubuntu 12.04(所有ubuntu发行版都适用)sudo免输入密码
首先执行以下命令(该命令用来修改 /etc/sudoers 文件): $ sudo gedit /etc/sudoers 然后把 %sudo ALL=(ALL:ALL) ALL 这行注释掉, ...
- 【stanford C++】字符串(String)与流(Stream)
字符串(String)与流(Stream) 一.C++中字符串(String) 字符串(String):就是(可能是空的)字符序列. C++中的字符串在概念上和Java中的字符串类似. C++字符串用 ...
- stata
1.只打开部分变量: use var1 var2 using "C:\data\2014.dta" 2.打开部分样本(5~10个样本) use "C:\data\2014 ...
- ClassLoader的等级加载机制
摘自深入分析java web技术内幕
- [oc] instancetype vs id for Objective-C 【转】
原贴地址:http://blog.csdn.net/lyy_whg/article/details/12846055 http://www.iwangke.me/2013/01/06/instance ...
- jdk 安装配置
在windows上配置JDK 1.下载windows版JDK 网址:http://www.oracle.com/technetwork/java/javase/archive-139210.html ...