Hard
Quiz
#458 Poor Pigs
APPROACH
One of buckets liquid buckets is poisonous. You can conduct multiple test rounds, each lasting minutesToDie minutes, within a total budget of minutesToTest minutes. In every round, pigs may drink from any combination of buckets simultaneously; pigs that consumed the poisonous bucket die at the end of the round.
Return the minimum number of pigs required to guarantee identifying the poisonous bucket before time runs out.
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.
1 of 4
1:00
What is the optimal approach for this problem?