作者: 负雪明烛
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)的更多相关文章

  1. Leetcode - 458 Poor Pigs

    题目: 总共有1000个罐子,其中有且只有1个是毒药,另外其他的都是水. 现在用一群可怜的猪去找到那个毒药罐. 已知毒药让猪毒发的时间是15分钟, 那么在60分钟之内,最少需要几头猪来找出那个毒药罐? ...

  2. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. Python基础之流程控制while循环

    目录 1. 语法 2. while+break 3. while+continue 4. while+else 1. 语法 最简单的while循环如下: ''' while <条件>: & ...

  2. Oracle-like 多条件过滤以及and or用法

    1.select * from  file  where DOC_SUBJECT  not like '%测试%' and (DOC_STATUS like '待审' or DOC_STATUS li ...

  3. java面试题目偏基础

    一.JAVA基础篇-概念1.简述你所知道的Linux:Linux起源于1991年,1995年流行起来的免费操作系统,目前, Linux是主流的服务器操作系统, 广泛应用于互联网.云计算.智能手机(An ...

  4. 巩固javaweb第十四天

    巩固内容: 单行文本框: 单行文本框的基本语法格式如下: < input type="text"  name="输入信息的字"  value=" ...

  5. Celery进阶

    Celery进阶 在你的应用中使用Celery 我们的项目 proj/__init__.py   /celery.py   /tasks.py 1 # celery.py 2 from celery ...

  6. Learning Spark中文版--第五章--加载保存数据(2)

    SequenceFiles(序列文件)   SequenceFile是Hadoop的一种由键值对小文件组成的流行的格式.SequenceFIle有同步标记,Spark可以寻找标记点,然后与记录边界重新 ...

  7. Spark(十)【RDD的读取和保存】

    目录 一.文件类型 1.Text文件 2.Json文件 3.对象文件 4.Sequence文件 二.文件系统 1. MySQL 2. Hbase 一.文件类型 1.Text文件 读写 读取 scala ...

  8. Spark(七)【RDD的持久化Cache和CheckPoint】

    RDD的持久化 1. RDD Cache缓存 ​ RDD通过Cache或者Persist方法将前面的计算结果缓存,默认情况下会把数据以缓存在JVM的堆内存中.但是并不是这两个方法被调用时立即缓存,而是 ...

  9. Linux磁盘分区(四)之分区大小调整

    Linux磁盘分区(四)之分区大小调整在学习调整分区大小之前,先了解linx分区的概念.参考如下博客:[1]linux 分区 物理卷 逻辑卷 https://www.cnblogs.com/liuch ...

  10. Lock锁的使用

    在Java多线程中,可以使用synchronized关键字实现线程之间的同步互斥,在jdk1.5后新增的ReentrantLock类同样可达到此效果,且在使用上比synchronized更加灵活. 观 ...