Poor Pigs
Problem
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
minutesToDieminutes. You may not feed any other pigs during this time. - After
minutesToDieminutes 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.
Examples
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 <= 10001 <= minutesToDie <= minutesToTest <= 100
Solution
Method 1 – Combinatorial Reasoning (Base Encoding)
Intuition
Each pig can be used multiple times, and each test round gives us information about which buckets are poisonous. If a pig survives all rounds, it means it never drank from the poisonous bucket. If it dies in a particular round, it means it drank poison in that round. By encoding the test results in base (T+1), where T is the number of test rounds, we can uniquely identify the poisonous bucket.
Approach
- Compute the number of test rounds:
T = minutesToTest // minutesToDie. - Each pig can encode
T+1states (dies in round 1, 2, ..., T, or survives all rounds). - With
ppigs, we can encode(T+1)^pbuckets. - Find the minimum
psuch that(T+1)^p >= buckets. - Return
ceil(log(buckets) / log(T+1)).
Code
C++
#include <cmath>
class Solution {
public:
int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
int T = minutesToTest / minutesToDie;
return ceil(log(buckets) / log(T + 1));
}
};
Go
import "math"
func poorPigs(buckets, minutesToDie, minutesToTest int) int {
T := minutesToTest / minutesToDie
return int(math.Ceil(math.Log(float64(buckets)) / math.Log(float64(T+1))))
}
Java
class Solution {
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
int T = minutesToTest / minutesToDie;
return (int) Math.ceil(Math.log(buckets) / Math.log(T + 1));
}
}
Kotlin
import kotlin.math.*
class Solution {
fun poorPigs(buckets: Int, minutesToDie: Int, minutesToTest: Int): Int {
val T = minutesToTest / minutesToDie
return ceil(ln(buckets.toDouble()) / ln((T + 1).toDouble())).toInt()
}
}
Python
import math
class Solution:
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
T = minutesToTest // minutesToDie
return math.ceil(math.log(buckets) / math.log(T + 1))
Rust
impl Solution {
pub fn poor_pigs(buckets: i32, minutes_to_die: i32, minutes_to_test: i32) -> i32 {
let t = minutes_to_test / minutes_to_die;
(f64::log(buckets as f64, (t + 1) as f64)).ceil() as i32
}
}
TypeScript
class Solution {
poorPigs(buckets: number, minutesToDie: number, minutesToTest: number): number {
const T = Math.floor(minutesToTest / minutesToDie);
return Math.ceil(Math.log(buckets) / Math.log(T + 1));
}
}
Complexity
- ⏰ Time complexity:
O(1), all operations are constant time. - 🧺 Space complexity:
O(1).