Problem Description:
There are 1000 buckets, of which there is only one barrel containing toxic drugs, and the rest are water. They all look the same from the look. If the pig drank the poison, it would die in 15 minutes.
The question is, if you need in an hour to figure out which bucket contains poison, how many pigs do you need at least?
Answer this question and write a general algorithm for the following advanced questions.
Advanced:
Suppose there are n buckets, pigs will die in the water after poisoning, how many pigs (x) will you need to find a "poisonous" bucket in P minutes? N There is only one poisonous bucket in the bucket.
Method: Suppose 5 barrels of water, one hours a pig test four barrels, if not dead, it must be the last barrel poisonous. Number of barrels to be detected by 5,POW (5,num) > buckets for each additional end
1 classsolution (object):2 defpoorpigs (self, buckets, Minutestodie, minutestotest):3 """4 : Type Buckets:int5 : Type Minutestodie:int6 : Type Minutestotest:int7 : Rtype:int8 """9Times = Minutestotest/minutestodie + 1#maximum number of buckets that can be tested per pigTennum =0 One whilePow (Times,num) <Buckets: Anum = num + 1 - returnNum
2018-10-03 21:47:29
leetcode--458--, poor little piggy.