zoukankan      html  css  js  c++  java
  • 458. Poor Pigs

    There are 1000 buckets, one and only one of them is poisonous, while the rest are filled with water. They all look identical. If a pig drinks the poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket is poisonous within one hour?

    Answer this question, and write an algorithm for the general case.

    General case:

    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 poisonous bucket within p minutes? There is exactly one bucket with poison.

    Note:

    1. A pig can be allowed to drink simultaneously on as many buckets as one would like, and the feeding takes no time.
    2. After a pig has instantly finished drinking buckets, there has to be a cool down time of minutes. During this time, only observation is allowed and no feedings at all.
    3. Any given bucket can be sampled an infinite number of times (by an unlimited number of pigs).

    Approach #1: Math. [Java]

    class Solution {
        public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
            int status = minutesToTest / minutesToDie + 1;
            int num_of_pig = 0;
            while (Math.pow(status, num_of_pig) < buckets) {
                num_of_pig++;
            }
            return num_of_pig;
        }
    }
    

      

    Analysis:

    With 2 pigs, poision killing in 15 minutes, and having 60 minutes, we can find the poison in up to 25 buckets in the following way. Arrange the buckets in 5*5 square:

     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

    Now use one pig to find the row (make it drink from buckets 1, 2, 3, 4, 5, wait 15 minutes, make it drink from buckets 6, 7, 8, 9, 10, wait 15 minutes, etc). Use the second pig to find the column (make it drink 1, 6, 11, 16, 21, then 2, 7, 12, 17, 22, etc).

    Having 60 minutes and tests taking 15 minutes means we can run four tests. If the row pig dies in the thrid test, the poison is in the third row. If the pig doesn't die at all, the posison is in the fifth column (this is why we can cover five rows / columns even though we can only run four tests).

    With 3 pigs, we can similarly use a 5 * 5 * 5 cube instead of a 5 * 5 square and again use one pig to determine the coordinate of one dimension (one pig drinks layers from top to bottom, one drinks layers from left to right, one drinks layers from front to back). So 3 pigs can solve up to 125 buckets.

    Reference:

    https://leetcode.com/problems/poor-pigs/discuss/94266/Another-explanation-and-solution

    https://blog.csdn.net/huanghanqian/article/details/77141719

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    (转) 建立自己的MemberShip数据库
    '??' 语法
    c# 静态构造函数(转)
    ReSharp+VAssistX+VS2003 的个人设置
    支持多种数据类型的ListView排序
    学习笔记
    Java实验报告(实验二)
    Java实验报告(实验一)
    java数组中null和空的区别。
    网页选项卡功能
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10859139.html
Copyright © 2011-2022 走看看