LeetCodee

458. Poor Pigs

Jump to Solution: Python Java C++ JavaScript C#

Problem Description

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. Each pig can feed from any number of buckets.
  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 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, if either pig dies, we can determine the poisonous bucket.
If neither pig dies, 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, neither pig should die. Feed the first pig bucket 3, and feed the second pig bucket 4.
At time 30, one of the pigs should die, and we can determine which bucket is poisonous.

Constraints:

  • 1 <= buckets <= 1000
  • 1 <= minutesToDie <= minutesToTest <= 100

Solution

Python Solution

class Solution:
    def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
        # Calculate number of tests possible
        tests = minutesToTest // minutesToDie
        
        # Each pig can represent tests + 1 states (including initial state)
        states_per_pig = tests + 1
        
        # Calculate minimum pigs needed using logarithm
        # states_per_pig ^ pigs >= buckets
        # pigs >= log(buckets) / log(states_per_pig)
        import math
        pigs = math.ceil(math.log(buckets) / math.log(states_per_pig))
        
        return pigs

Time Complexity: O(1)

The solution uses simple mathematical operations.

Space Complexity: O(1)

Only constant extra space is used.

Java Solution

class Solution {
    public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        // Calculate number of tests possible
        int tests = minutesToTest / minutesToDie;
        
        // Each pig can represent tests + 1 states (including initial state)
        int statesPerPig = tests + 1;
        
        // Calculate minimum pigs needed using logarithm
        // statesPerPig ^ pigs >= buckets
        // pigs >= log(buckets) / log(statesPerPig)
        return (int) Math.ceil(Math.log(buckets) / Math.log(statesPerPig));
    }
}

Time Complexity: O(1)

The solution uses simple mathematical operations.

Space Complexity: O(1)

Only constant extra space is used.

C++ Solution

class Solution {
public:
    int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        // Calculate number of tests possible
        int tests = minutesToTest / minutesToDie;
        
        // Each pig can represent tests + 1 states (including initial state)
        int statesPerPig = tests + 1;
        
        // Calculate minimum pigs needed using logarithm
        // statesPerPig ^ pigs >= buckets
        // pigs >= log(buckets) / log(statesPerPig)
        return ceil(log(buckets) / log(statesPerPig));
    }
};

Time Complexity: O(1)

The solution uses simple mathematical operations.

Space Complexity: O(1)

Only constant extra space is used.

JavaScript Solution

/**
 * @param {number} buckets
 * @param {number} minutesToDie
 * @param {number} minutesToTest
 * @return {number}
 */
var poorPigs = function(buckets, minutesToDie, minutesToTest) {
    // Calculate number of tests possible
    const tests = Math.floor(minutesToTest / minutesToDie);
    
    // Each pig can represent tests + 1 states (including initial state)
    const statesPerPig = tests + 1;
    
    // Calculate minimum pigs needed using logarithm
    // statesPerPig ^ pigs >= buckets
    // pigs >= log(buckets) / log(statesPerPig)
    return Math.ceil(Math.log(buckets) / Math.log(statesPerPig));
};

Time Complexity: O(1)

The solution uses simple mathematical operations.

Space Complexity: O(1)

Only constant extra space is used.

C# Solution

public class Solution {
    public int PoorPigs(int buckets, int minutesToDie, int minutesToTest) {
        // Calculate number of tests possible
        int tests = minutesToTest / minutesToDie;
        
        // Each pig can represent tests + 1 states (including initial state)
        int statesPerPig = tests + 1;
        
        // Calculate minimum pigs needed using logarithm
        // statesPerPig ^ pigs >= buckets
        // pigs >= log(buckets) / log(statesPerPig)
        return (int)Math.Ceiling(Math.Log(buckets) / Math.Log(statesPerPig));
    }
}

Time Complexity: O(1)

The solution uses simple mathematical operations.

Space Complexity: O(1)

Only constant extra space is used.

Approach Explanation

The solution uses information theory and combinatorics:

  1. Key Insights:
    • Information encoding
    • State representation
    • Base conversion
    • Logarithmic solution
  2. Algorithm Steps:
    • Calculate test rounds
    • Determine states per pig
    • Apply logarithm
    • Round up result

Implementation Details:

  • Mathematical formula
  • Logarithm usage
  • Ceiling function
  • Integer division

Optimization Insights:

  • Constant time
  • No iteration needed
  • Direct calculation
  • Memory efficient

Edge Cases:

  • Single bucket
  • Equal times
  • Maximum buckets
  • Minimum times