[leetcode]458. Poor Pigs
Analysis
待会要考政治—— [生死有命富贵在天!!!]
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.
很巧妙的一道题,https://blog.csdn.net/wilschan0201/article/details/72519147 这位大佬分析的超清楚~
Implement
class Solution {
public:
int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
if(buckets <= 1)
return 0;
int k = minutesToTest / minutesToDie + 1;
int res = 1;
while(res <= buckets){
if(pow(k, res) >= buckets)
return res;
res++;
}
}
};