1. 程式人生 > >Poor Pigs:測試毒藥

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.

這道題是真難啊!!!其中一個樣例是:
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;
    }
}