转自[LeetCode] Poor Pigs 可怜的猪

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.

猪只能喝一次

假设只有1只猪,只有15分钟,那么我们能测几个水桶呢?很显然是两个,因为只能测一次的话,让猪去随便喝一桶,如果毒死了,就是喝的那桶,反之则是另一桶。

如果有两只猪呢,能测几桶?怎么喝呢,两只猪一猪喝一桶,再同时喝一桶,剩下一桶谁也不喝,那么如果两只猪都毒死了,说明是共同喝的那桶有毒,如果某个猪毒死了,说明该猪喝的那桶有毒,如果都没事,说明是谁也没喝的那桶。

三猪能测8桶,其实就是2的指数倍。第一只猪喝1、4、5、7桶,第二只2、4、6、7,第三只3、5、6、7。即\(1+C_3^1+C_3^2+C_3^3+1=2^3\)

如果只能测一次的话,实际上相当一个一维数组,而如果能测两次的话,情况就不一样了,我们就可以重复利用猪了。

比如还是两只猪,能测两次,功能测几个桶,答案可以测9桶,为啥,我们组个二维数组:

1 2 3

4 5 6

7 8 9

如果我们让第一头猪第一次喝1,2,3桶,第二次喝4,5,6桶,而让第二头猪第一次喝1,4,7桶,第二次喝2,5,8桶,我们可以根据猪的死亡情况来确定是哪一桶的问题,实际上就把猪被毒死的那个节点当作了二维数组的横纵坐标来定位毒桶的位置,巧妙吧~更巧妙的是,如果再增加一头猪,实际上是给数组增加一个维度,变成了一个三维数组,那么三只猪,测两次,可以测27桶,叼不叼。这道题让我们求最少用多少猪来测,那么就是求数组的维度,我们知道了数组的总个数,所以要尽量增加数组的长宽,尽量减少维度。这里,数组的长宽其实都是测试的次数+1,所以我们首先要确定能测的次数,通过总测试时间除以毒发时间,再加上1就是测试次数。有了数组长宽m,那么如果有x只猪,能测的桶数为m的x次方,现在我们给定了桶数N,要求x,就log一下就行,然后用个换底公式,就可以求出x的值了,参见代码如下:

class Solution {
public:
int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
return ceil(log(buckets) / log(minutesToTest / minutesToDie + 1));
}
};

1228.Poor Pigs 可怜的猪的更多相关文章

  1. [LeetCode] Poor Pigs 可怜的猪

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  2. Leetcode458.Poor Pigs可怜的小猪

    有1000只水桶,其中有且只有一桶装的含有毒药,其余装的都是水.它们从外观看起来都一样.如果小猪喝了毒药,它会在15分钟内死去. 问题来了,如果需要你在一小时内,弄清楚哪只水桶含有毒药,你最少需要多少 ...

  3. Leetcode - 458 Poor Pigs

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

  4. [Swift]LeetCode458. 可怜的小猪 | Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  5. 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  6. 【LeetCode】458. Poor Pigs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. Leetcode: Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  8. LeetCode算法题-Poor Pigs(Java实现)

    这是悦乐书的第235次更新,第248篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第102题(顺位题号是455).有1000个水桶,其中只有一个水桶含有毒药,其余的都没毒 ...

  9. [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 ...

随机推荐

  1. Win7 下安装ubuntu14.04双系统

    下面介绍一下利用wubi在Windows中安装Ubuntu 14.04的教程,或者说安装方法和注意事项.  方法一:直接下载wubi.exe 方法二:直接下载ubuntu-14.04-desktop- ...

  2. JWT 认证 以及Django 中的应用

    jwt 认证 私钥.公钥.CA认证 用一套加密规则 加密和解密 RSA加密 (非对称的加密) 摘要算法:MD5 FTP/互联网下载软件校验MD5 私钥 --RSA算法-->公钥 RSA原理 加密 ...

  3. 15.selenium_case02

    # 抓取今日头条的新闻链接 from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWa ...

  4. maven 经常使用命令

    版权声明:本文为博主原创文章,未经博主同意不得转载. 安金龙 的博客. https://blog.csdn.net/smile0198/article/details/25567541 刚開始用.记录 ...

  5. 解决y7000笔记本ubuntu18.04下 休眠挂起后唤醒花屏

    定位问题,切换到核显后发现一点问题也没有,基本确定是显卡驱动的问题 但是由于配置环境比较复杂,不想重新装N卡驱动,所以另寻方法 sudo gedit /etc/default/grub 修改前 # I ...

  6. numpy中矩阵乘法,星乘(*)和点乘(.dot)的区别

    import numpy a = numpy.array([[,], [,]]) b = numpy.array([[,], [,]]) 星乘表示矩阵内各对应位置相乘,矩阵a*b下标(0,0)=矩阵a ...

  7. @property和@score.setter的用法

    @property 把属性装饰成get方法 给属性赋值时,会自动调用@property装饰的方法 只设置属性的@property 时,属性为只读 @score.setter 把属性装饰成set方法 给 ...

  8. StringRedisTemplate操作Redis

    在说到StringRedisTemplate操作Redis数据的时候,我们顺便谈谈StringRedisTemplate和RedisTemplate的区别. 一.StringRedisTemplate ...

  9. 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())

    package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...

  10. OpenCV3计算机视觉Python语言实现笔记(五)

    图像的几何变换主要包括:平移.扩大与缩小.旋转.仿射.透视等等.图像变换是建立在矩阵运算基础上的,通过矩阵运算可以很快的找到对应关系. 1. 图像的平移 图像的平移,沿着x方向tx距离,y方向ty距离 ...