Poor Pigs LeetCode Solution

Last updated on March 2nd, 2025 at 02:34 pm

Here, we see a Poor Pigs LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

List of all LeetCode Solution

Topics

Math

Level of Question

Hard

Poor Pigs LeetCode Solution

Poor Pigs LeetCode Solution

1. Problem Statement

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, and each bucket can be fed from by any number of pigs.
  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.

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.

2. Coding Pattern Used in Solution

The coding pattern can be categorized under a “Mathematical Simulation” pattern. This pattern involves simulating a mathematical or logical process to derive the solution.

3. Code Implementation in Different Languages

3.1 Poor Pigs C++

class Solution {
public:
    int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        return ceil(log2(buckets)/log2(int(minutesToTest/minutesToDie)+1));        
    }
};

3.2 Poor Pigs Java

class Solution {
    public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
        int test = minutesToTest/minutesToDie;
        int i=0;
        while(Math.pow(test+1,i)< buckets)
        {
            i++;
        }
        return i;        
    }
}

3.3 Poor Pigs JavaScript

var poorPigs = function(buckets, minutesToDie, minutesToTest) {
    let max_time = minutesToTest / minutesToDie + 1;
    let req_pigs = 0;
    while (Math.pow(max_time, req_pigs) < buckets)
        ++req_pigs;
    return req_pigs;
};

3.4 Poor Pigs Python

class Solution(object):
    def poorPigs(self, buckets, minutesToDie, minutesToTest):
        max_time = minutesToTest / minutesToDie + 1.
        req_pigs = 0
        while (max_time) ** req_pigs < buckets:
            req_pigs += 1
        return req_pigs

4. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(1)O(1)
JavaO(log(n))O(1)
JavaScriptO(log(n))O(1)
PythonO(log(n))O(1)

where n is the number of buckets.

Scroll to Top