Poor Pigs LeetCode Solution

Here, We see Poor Pigs LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Poor Pigs LeetCode Solution

Poor Pigs LeetCode Solution

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.

Poor Pigs LeetCode Solution C++

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

Poor Pigs LeetCode Solution 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;        
    }
}Code language: JavaScript (javascript)

Poor Pigs LeetCode Solution 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;
};Code language: JavaScript (javascript)

Poor Pigs LeetCode Solution 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_pigsCode language: HTML, XML (xml)
Scroll to Top