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.
知道哪桶水是有毒的信息量是:log(1000, 2)
从一头猪身上得到的信息量:log(4+1,2),总共有5中可能
结果:log(1000, 2)/log(4+1,2) 向上取整
class Solution {
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
return (int) Math.ceil(Math.log(buckets)/Math.log(2) / (Math.log(minutesToTest/minutesToDie+1)/Math.log(2)));
}
}