0458. Poor Pigs

458. Poor Pigs#

题目 #

There are buckets buckets of liquid, where exactly one of the buckets is poisonous. To figure out which one is poisonous, you feed some number of (poor) pigs the liquid to see whether they will die or not. Unfortunately, you only have minutesToTest minutes to determine which bucket is poisonous.

You can feed the pigs according to these steps:

• Choose some live pigs to feed.
• For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.
• Wait for minutesToDie minutes. You may not feed any other pigs during this time.
• After minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
• Repeat this process until you run out of time.

Given buckets, minutesToDie, and minutesToTest, return the minimum number of pigs needed to figure out which bucket is poisonous within the allotted time.

Example 1:

``````Input: buckets = 1000, minutesToDie = 15, minutesToTest = 60
Output: 5
``````

Example 2:

``````Input: buckets = 4, minutesToDie = 15, minutesToTest = 15
Output: 2
``````

Example 3:

``````Input: buckets = 4, minutesToDie = 15, minutesToTest = 30
Output: 2
``````

Constraints:

• 1 <= buckets <= 1000
• 1 <= minutesToDie <= minutesToTest <= 100

题目大意 #

• 选择若干活猪进行喂养
• 可以允许小猪同时饮用任意数量的桶中的水，并且该过程不需要时间。
• 小猪喝完水后，必须有 minutesToDie 分钟的冷却时间。在这段时间里，你只能观察，而不允许继续喂猪。
• 过了 minutesToDie 分钟后，所有喝到毒药的猪都会死去，其他所有猪都会活下来。
• 重复这一过程，直到时间用完。

解题思路 #

• 0-15 小猪吃第一个桶中的液体,如果死去，则第一个桶有毒,否则继续测试
• 15-30 小猪吃第二个桶中的液体，如果死去，则第二个桶有毒,否则继续测试
• 30-45 小猪吃第三个桶中的液体，如果死去，则第三个桶有毒,否则继续测试
• 45-60 小猪吃第四个桶中的液体，如果死去，则第四个桶有毒
• 如果最后小猪没有死去，则第五个桶有毒

代码 #

``````
package leetcode

import "math"

func poorPigs(buckets int, minutesToDie int, minutesToTest int) int {
base := minutesToTest/minutesToDie + 1
return int(math.Ceil(math.Log10(float64(buckets)) / math.Log10(float64(base))))
}
``````

Apr 8, 2023