【LeetCode】458. Poor Pigs 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/poor-pigs/description/
题目描述
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.
题目大意
有1000个桶,其中有一个桶里有毒药,其他的都是正常的。一头猪喝了毒药之后,过了15分钟会上天。只给了60分钟,问至少需要多少头猪来找出有毒的桶?
下面给了一个Follow-up,是桶数(n),上天时间为m,在p分钟内检测出来,求猪数x。
解题方法
这个题让我想起来本科的时候的微机原理,那个时候有题目是拨号键盘。通过监测拨号键盘行和列有哪些有电平,来检测出按下了哪个键。
一只猪在一个小时内最多验多少桶呢?这个猪可以喝4桶水,如果有毒就会死亡,否则说明这4桶没问题。如果只有5桶水,那么第5桶一定有问题。
两只猪呢?那就是拨号键盘了,每15分钟喝一个行或者列的所有5桶水,根据两只猪死亡时间,按照拨号键盘的思路,知道了毒水在几行几列。也就是两头猪最多验出来25个桶。
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
更多的猪,不难看出,就是一个多维的空间了,当有N只的时候,求出5^N>=1000,就能得出了N。
代码如下:
class Solution(object):
def poorPigs(self, buckets, minutesToDie, minutesToTest):
"""
:type buckets: int
:type minutesToDie: int
:type minutesToTest: int
:rtype: int
"""
tests = minutesToTest / minutesToDie + 1
pigs = 0
while tests ** pigs < buckets:
pigs += 1
return pigs
参考资料:https://blog.csdn.net/wilschan0201/article/details/72519147
日期
2018 年 7 月 17 日 —— 连天大雨,这种情况很少见,但是很舒服
2018 年 11 月 19 日 —— 周一又开始了
【LeetCode】458. Poor Pigs 解题报告(Python)的更多相关文章
- Leetcode - 458 Poor Pigs
题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- Prometheus_exporter安装与使用
Promethues概述:可以看一下更详细的介绍,以下为转载的博客,原文链接,支持原创,请多多支持!!:闫世成的博客园 Prometheus-node-exporter 1.简介: 内核公开的硬件和操 ...
- 零基础学习java------33---------http协议,tomcat(其如何在eclipse上发布),注册案例
一. HTTP协议 https://www.cnblogs.com/vamei/archive/2013/05/11/3069788.html 二. tomcat---------->web服务 ...
- nodejs-CommonJS规范
JavaScript 标准参考教程(alpha) 草稿二:Node.js CommonJS规范 GitHub TOP CommonJS规范 来自<JavaScript 标准参考教程(alpha) ...
- JS去除对象或数组中的空值('',null,undefined,[],{})
javascript去掉对象或数组中的'',null,undefined,[],{}.思路就是创建一个新的空对象,然后对传入的对象进行遍历,只把符合条件的属性返回,保留有效值,然后就相当于把空值去掉了 ...
- Dos窗口下中文乱码问题
最近用Datax工具进行数据同步时,在DOS窗口下出现了中文乱码问题,导致一些错误只能到Log中查看,在网上找了一些方法,记录使用成功的方法. Dos命令:chcp 通过cmd进入Dos命令窗口,执行 ...
- android Paint 详解
/** * Paint类介绍 * * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色, * 样式等绘制信息,指定了如何绘制文本和图形,画笔对象有很多设置方法, * 大体 ...
- spring注解-组件注册
一.@Configuration+@Bean @Configuration:配置类==配置文件 @Bean:给容器中注册一个Bean:类型为返回值的类型,默认是用方法名作为id @Bean(" ...
- Servlet(1):Servlet介绍
一. Servlet介绍 Servlet 是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据,生 ...
- 【Linux】【Commands】文件管理工具
文件管理工具:cp, mv, rm cp命令:copy 源文件:目标文件 单源复制:cp [OPTION]... [-T] SOURCE DEST 多源复制:cp [OPTION]... SOURCE ...
- Java Bean 与Spring Bean 的区别
什么是JavaBean: JavaBean是一种JAVA语言写的可重用组件.JavaBean符合一定规范写的Java类,是一种规范.它的方法命名,构造以及行为必须符合特定的要求: 1.所有属性 ...