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:
minutesToDie
minutes. You may not feed any other pigs during this time.minutesToDie
minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.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 = 4, minutesToDie = 15, minutesToTest = 15 Output: 2 Explanation: We can determine the poisonous bucket as follows: At time 0, feed the first pig buckets 1 and 2, and feed the second pig buckets 2 and 3. At time 15, there are 4 possible outcomes: - If only the first pig dies, then bucket 1 must be poisonous. - If only the second pig dies, then bucket 3 must be poisonous. - If both pigs die, then bucket 2 must be poisonous. - If neither pig dies, then bucket 4 must be poisonous.
Example 2:
Input: buckets = 4, minutesToDie = 15, minutesToTest = 30 Output: 2 Explanation: We can determine the poisonous bucket as follows: At time 0, feed the first pig bucket 1, and feed the second pig bucket 2. At time 15, there are 2 possible outcomes: - If either pig dies, then the poisonous bucket is the one it was fed. - If neither pig dies, then feed the first pig bucket 3, and feed the second pig bucket 4. At time 30, one of the two pigs must die, and the poisonous bucket is the one it was fed.
Constraints:
1 <= buckets <= 1000
1 <= minutesToDie <= minutesToTest <= 100
This problem asks to find the minimum number of pigs needed to identify a poisonous bucket out of buckets
buckets within minutesToTest
minutes, given that it takes minutesToDie
minutes for a pig to die after consuming poison.
The core idea is to leverage the number of possible test outcomes. Each pig can be given a subset of the buckets, and whether it lives or dies provides information about which bucket is poisonous. With pigs
pigs, each pig can have 2 outcomes (lives or dies), resulting in 2^pigs
possible outcomes. These outcomes must be at least as many as the number of buckets to uniquely identify the poisonous bucket.
Mathematical Approach
Let's define:
base
: The number of tests we can perform per pig. This is determined by the number of time intervals available (minutesToTest // minutesToDie + 1
). We add 1 because if the test period allows for more than one test, we want to count the initial time as a test opportunity.
pigs
: The number of pigs we need.
The total number of possible test outcomes is base^pigs
. To be able to identify any of the buckets
, we need:
base^pigs >= buckets
We can solve for pigs
using logarithms:
pigs >= log(buckets) / log(base)
Since pigs
must be an integer, we round up the result to the nearest integer.
Code Implementation (Python3 as an example)
The code directly implements the mathematical solution described above:
class Solution:
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
base = minutesToTest // minutesToDie + 1
res, p = 0, 1
while p < buckets:
p *= base
res += 1
return res
Time and Space Complexity Analysis
Time Complexity: O(log(buckets)), The loop iterates at most log(buckets) (base base
) times, where the logarithm's base is the number of tests per pig. This is because the number of possible combinations increases exponentially with the number of pigs.
Space Complexity: O(1), The solution uses a constant amount of extra space regardless of the input size.
Other Language Implementations
The logic remains identical across different programming languages. The only differences are in syntax and library functions used. The provided Java, C++, and Go solutions all reflect the same mathematical approach and have the same time and space complexity characteristics as the Python code. The iterative approach avoids the need for explicit logarithm calculations, making it highly efficient for this problem.