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 ...
随机推荐
- 基于easyui的webform扩展(续)
基于easyui的webform扩展(续) 回顾 <前端基于easyui的mvc扩展>.<前端基于easyui的mvc扩展(续)>.<基于easyui的webform扩展 ...
- 推荐一些C#相关的网站、资源和书籍 (转载自http://www.cnblogs.com/jiangxiaofan/p/3808316.html)
推荐一些C#相关的网站.资源和书籍 (转载自http://blog.csdn.net/chinacsharper/article/details/17514923) 一.网站 1.http://m ...
- 为什么不能在子类或外部发布C#事件
为什么不能在子类或外部发布C#事件 背景 一个朋友问了一个问题:“为什么不能在子类或外部发布C#事件?”,我说我不知道,要看看生产的IL代码,下面我们看看. 测试 代码 1 using System; ...
- WCF服务承载
WCF服务承载(笔记) 自托管(也做自承载) 承载 WCF 服务最灵活.最便捷的方法就是进行自承载.要能够自承载服务,必须满足两个条件.第一,需要 WCF 运行时:第二,需要可以承载 Servic ...
- cocos2d-x protobuf; cocos2dx protocol buffer
昨天了解到项目要用到protocol buffer,今天晚上看了一下,了解protobuf本质上就是一个信息表达协议+编辑,解析库. linux开源软件都一个模式,先./configure --hel ...
- 死锁线程探讨Java中的死锁现象
题记:写这篇博客要主是加深自己对死锁线程的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 今天搞了一下Java的死锁机制,感到自己还是不怎么懂,所以就从一些简略的源代码中琢磨:我先 ...
- mybatis中updateByPrimaryKeySelective
mybatis中updateByPrimaryKeySelective等选择性操作在判断时对于VARCHAR类型需要同时判断非空和非空串 <if test="description ! ...
- Android libyuv应用系列(二)libyuv的使用
上篇文章Android libyuv使用系列(一)Android常用的几种格式:NV21/NV12/YV12/YUV420P的区别中我们了解了YUV相关的知识,而本篇文章我会介绍libyuv是什么,以 ...
- PHP绿色集成环境在云服务器上的应用,PHPWAMP在服务器上搭建网站案例
问:什么叫WAMP?答:Windows下的Apache+Mysql+PHP,称之为WAMP. 本文案例采用的PHP集成环境是我自己开发的纯绿色版WAMP软件(PHPWAMP). 我在这款集成环境里集成 ...
- jq 测试是否到页面最底端
$(window).scroll(function () { if ($(document).scrollTop() + $(window).height() >= $(document).he ...