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 withinm
minutes, how many pigs (x
) you need to figure out the poisonous bucket withinp
minutes? There is exactly one bucket with poison.
Note:
- A pig can be allowed to drink simultaneously on as many buckets as one would like, and the feeding takes no time.
- After a pig has instantly finished drinking buckets, there has to be a cool down time of m minutes. During this time, only observation is allowed and no feedings at all.
- 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