0458. Poor Pigs (H)

Poor Pigs (H)

題目

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?html

Answer this question, and write an algorithm for the general case.java

General case:ide

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.this

Note:rest

  1. A pig can be allowed to drink simultaneously on as many buckets as one would like, and the feeding takes no time.
  2. 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.
  3. Any given bucket can be sampled an infinite number of times (by an unlimited number of pigs).

題意

用盡量少的🐖在規定時間內從若干水桶中找到惟一有毒的那一桶。code

思路

其實是個數學問題。參考[LeetCode] Poor Pigs 可憐的豬htm


代碼實現

Java

class Solution {
    public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        return (int) Math.ceil(Math.log(buckets) / Math.log(minutesToTest / minutesToDie + 1));
    }
}
相關文章
相關標籤/搜索