描述
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.
挑战
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.
这道题十分有趣。。。有趣到让我就觉得这不是算法题,我开始不太能想通该怎么算,然后就查该怎么做。
看了一些解释的棒的博客:http://www.cnblogs.com/grandyang/p/7664088.html
才终于明白了怎么做,这个维度确实是很难理解。
class Solution {
public:
/**
* @param buckets: an integer
* @param minutesToDie: an integer
* @param minutesToTest: an integer
* @return: how many pigs you need to figure out the "poison" bucket within p minutes
*/
int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
// Write your code here
return ceil(log(buckets) / log(minutesToTest / minutesToDie + 1));
}
};