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)
  • 相关阅读:
    【Mybatis】 JdbcType 与 JavaType对应关系
    【入门知识】虚拟机化概念
    【Ubuntu 16】安装deb
    【Ubuntu 16】安装eclipse
    【刷题】HDU 4405 Aeroplane chess
    【刷题】BZOJ 3998 [TJOI2015]弦论
    【刷题】SPOJ 1812 LCS2
    【刷题】BZOJ 2882 工艺
    【刷题】UOJ #171 【WC2016】挑战NPC
    【刷题】UOJ #79 一般图最大匹配
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10859139.html
Copyright © 2011-2022 走看看