Poor Pigs:测试毒药

转载来源: https://leetcode.com/problems/poor-pigs/discuss/94278

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
Expected answer
5
崩溃!之前看师姐的笔试题里有类似的题。这是一道规律题。

设猪的数目N  ,可以测试的次数T,总共可以测试的桶数C,那么:

T = 1,N = 1 ,C = 2 

T = 1,N = 2 ,C = 4

T = 1,N = 3 ,C = 8

T = 1,N = N,C = 2^N

T = 2,N = N,C = (2+1)^N

T = T,N = N,C = (T+1)^N  。

下面内容来自转载:

Let’s think in another way:
If we get N pigs and can test T times, what is the maximum number of buckets that we can successfully detect the poison among them?


Here we take T=1 (can only test once) and N=2 (2 pigs) as example:

    x -> not drink      o -> drink

T=1  N=2:

   buckets       1    2    3    4
     pig 1       x    x    o    o
     pig 2       x    o    x    o

 Result:   2 pigs and test 1 times -> 4 buckets

Conclusion T=1 N=n:

n pigs and test 1 times can deal with how many buckets

= 2^n


We take T=2 (can only test twice) and N=2 (2 pigs) as example:

T=2  N=2:

   buckets       1    2    3    4    5    6    7    8    9  
     pig 1       o    x    x    o    o    x    x    x    x    
     pig 2       o    o    o    x    x    x    x    x    x
                                                  
Result:   2 pigs and test 2 times --> 9 buckets

Explain:   
  pig 1 & 2 died     ->  1 possible bucket
  pig 1 died only    ->  2 possible buckets  -> 1 pig and 1 times can deal with 2 buckets(straight-forward)
  pig 2 died only    ->  2 possible buckets  -> 1 pig and 1 times can deal with 2 buckets(straight-forward)
  pig 1 & 2 survived ->  4 possible buckets  -> 2 pigs and 1 times can deal with 4 buckets(Previous case: T=1 N=2)

Conclusion T=2 N=n:

n pigs and test 2 times can deal with how many buckets

= C(n,n) * 2^0 + C(n,n-1) * 2^1 + … + C(n,0) * 2^n
= (1+2)^n
= 3^n

Explain:
#(all pigs died) + #(1 pigs survived) * can test 2 buckets(T=1 N=1) + #(2 pigs survived) * can test 4 buckets(T=1 N=2) + … + #(n pigs survived) * can test 2^n buckets(T=1 N=n)


Now, try to think about the case T=3 (can test 3 times) and N=n (n pigs)

Conclusion (T=3 N=n):

n pigs and test 3 times can deal with how many buckets

= C(n,n) * 3^0 + C(n,n-1) * 3^1 + … + C(n,0) * 3^n
= (1+3)^n
= 4^n

Explain:
#(all pigs died) + #(1 pigs survived) * can test 3 buckets(T=2 N=1) + #(2 pigs survived) * can test 9 buckets(T=2 N=2) + … + #(n pigs survived) * can test 3^n buckets(T=2 N=n)


To sum it up,

If we get N pig and can test T times, the maximum number of buckets that we can successfully detect the poison among them is ----> (1+T)^N.

Therefore, the answer to the question is:

 MIN(n),   where (1+T)^n >= number of buckets    
                   Note: T = minutesToTest/minutesToDie
class Solution {
    public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        int T = minutesToTest/minutesToDie;
        int res = 0;
        while(Math.pow(T+1,res)<buckets) res++;
        return res;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值