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 within m
minutes, how many pigs (x
) you need to figure out the poisonous bucket within p
minutes? There is exactly one bucket with poison.
Note:
用尽可能少的??在规定时间内从若干水桶中找到唯一有毒的那一桶。
实际上是个数学问题。参考[LeetCode] Poor Pigs 可怜的猪。
class Solution {
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
return (int) Math.ceil(Math.log(buckets) / Math.log(minutesToTest / minutesToDie + 1));
}
}
原文:https://www.cnblogs.com/mapoos/p/13973969.html