Watch 10 video solutions for Poor Pigs, a hard level problem involving Math, Dynamic Programming, Combinatorics. This walkthrough by Coding Decoded has 10,740 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 <= 10001 <= minutesToDie <= minutesToTest <= 100Problem Overview: You have buckets buckets where exactly one is poisoned. A pig dies minutesToDie minutes after drinking poison, and you have minutesToTest minutes total. Multiple rounds of testing are possible. The task is to compute the minimum number of pigs required to guarantee identifying the poisoned bucket.
Approach 1: Binary Strategy (O(log n) time, O(1) space)
This approach treats each pig as a binary indicator across testing rounds. If a pig drinks from a bucket and dies during a specific round, that timing encodes information about which bucket was poisoned. With r = minutesToTest / minutesToDie rounds, each pig effectively contributes a bit across multiple time intervals. By assigning buckets to combinations of pigs and rounds, you can uniquely encode bucket identities. The number of pigs needed grows logarithmically with buckets. This interpretation works well if you view pigs as bits in a positional encoding scheme.
Approach 2: State Expansion Strategy (O(p * r) time, O(p) space)
Instead of binary outcomes, observe that a pig has multiple possible states: it may die in round 1, round 2, ..., round r, or survive all rounds. That gives r + 1 states per pig. With multiple pigs, the total distinguishable outcomes become (r + 1)^p. The algorithm incrementally increases the number of pigs until the total states cover all buckets. This reasoning resembles state expansion used in dynamic programming, where combinations of states encode the final answer.
Approach 3: Mathematical Representation Using States (O(1) time, O(1) space)
The key insight is that pigs form a positional number system where each pig represents a digit with base (r + 1). If a pig dies in round i, that digit equals i. If it survives, the digit equals r. With p pigs you can encode (r + 1)^p unique outcomes, which must be at least the number of buckets. Solve the inequality (r + 1)^p >= buckets and compute p = ceil(log(buckets) / log(r + 1)). This mathematical approach leverages concepts from math and combinatorics, and it produces the cleanest constant-time solution.
Approach 4: Binary Search for Minimum Pigs (O(log buckets) time, O(1) space)
Another practical method is to binary search the number of pigs. For a candidate p, compute how many buckets can be tested using (r + 1)^p. If that value is smaller than the required buckets, increase p; otherwise reduce it. This avoids logarithm formulas and is convenient in languages where precision or overflow may matter.
Recommended for interviews: The mathematical state representation is the expected solution. Interviewers look for the insight that each pig contributes r + 1 distinguishable states, turning the problem into a combinatorial encoding problem. Explaining the state reasoning first and then deriving (r + 1)^p >= buckets demonstrates strong problem-solving ability.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Binary Strategy | O(log buckets) | O(1) | When reasoning about pigs as binary indicators across rounds |
| State Expansion Strategy | O(p * r) | O(p) | When incrementally exploring how many outcomes pigs can encode |
| Mathematical Representation Using States | O(1) | O(1) | Best approach for interviews and production solutions |
| Binary Search for Minimum Pigs | O(log buckets) | O(1) | When avoiding logarithmic formulas or handling large numbers safely |