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:

  1. Choose some live pigs to feed.
  2. For each pig, choose which buckets to feed it. The pig will consume all the chosen buckets simultaneously and will take no time.
  3. Wait for minutesToDie minutes. You may not feed any other pigs during this time.
  4. After minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
  5. Repeat this process until you run out of time.

Given bucketsminutesToDie, and minutesToTest, return the minimum number of pigs needed to figure out which bucket is poisonous within the allotted time.

Examples

Example 1:

1
2
3
4
Input:
buckets = 1000, minutesToDie = 15, minutesToTest = 60
Output:
 5

Example 2:

1
2
3
4
Input:
buckets = 4, minutesToDie = 15, minutesToTest = 15
Output:
 2

Example 3:

1
2
3
4
Input:
buckets = 4, minutesToDie = 15, minutesToTest = 30
Output:
 2

Constraints:

  • 1 <= buckets <= 1000
  • 1 <= 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

  1. Compute the number of test rounds: T = minutesToTest // minutesToDie.
  2. Each pig can encode T+1 states (dies in round 1, 2, …, T, or survives all rounds).
  3. With p pigs, we can encode (T+1)^p buckets.
  4. Find the minimum p such that (T+1)^p >= buckets.
  5. Return ceil(log(buckets) / log(T+1)).

Code

1
2
3
4
5
6
7
8
#include <cmath>
class Solution {
public:
    int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        int T = minutesToTest / minutesToDie;
        return ceil(log(buckets) / log(T + 1));
    }
};
1
2
3
4
5
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))))
}
1
2
3
4
5
6
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));
    }
}
1
2
3
4
5
6
7
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()
    }
}
1
2
3
4
5
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))
1
2
3
4
5
6
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
    }
}
1
2
3
4
5
6
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).