Poor Pigs Problem
Description
LeetCode Problem 458.
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
Solution
Approach: Count Outcomes per Pig
There is time for rounds = minutesToTest / minutesToDie rounds of feeding (rounded down). Instead of asking which buckets to feed, ask how much information one pig gives. At the end of the test a pig has either died after round 1, after round 2, ..., after round rounds, or survived them all. That is outcomes = rounds + 1 different results.
With p pigs the combined results can take outcomesp values, so at most that many buckets can be told apart. And that many always can be: number the buckets in base outcomes with p digits (each from 0 to rounds), one digit per pig. In round r, each pig drinks from every bucket whose digit for that pig is r. A pig that dies in round r reveals that digit of the poisoned bucket as r; a pig that survives everything reveals the digit 0. Together the pigs spell out the bucket's number.
So the answer is the smallest p with outcomesp ≥ buckets. For buckets = 4, minutesToDie = 15, minutesToTest = 15 there is one round and 2 outcomes per pig, and 22 = 4, so 2 pigs.
Compute it with a loop of integer multiplications rather than ceil(log(buckets) / log(outcomes)), which can be off by one when buckets is an exact power and the floating-point division lands just above an integer. With one bucket the answer is 0: nothing needs to be tested.
Complexity: O(log buckets) time and O(1) space.
class Solution:
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
# A pig can die after round 1, 2, ..., or survive every round: that is rounds + 1 outcomes.
outcomes = minutesToTest // minutesToDie + 1
pigs = 0
covered = 1 # how many buckets `pigs` pigs can tell apart: outcomes ** pigs
while covered < buckets:
pigs += 1
covered *= outcomes
return pigs
Now write it from memory
Close the write-up and solve it yourself against the tests.