458. Poor Pigs LeetCode Solution

In this guide, you will get 458. Poor Pigs LeetCode Solution with the best time and space complexity. The solution to Poor Pigs problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Poor Pigs solution in C++
  4. Poor Pigs solution in Java
  5. Poor Pigs solution in Python
  6. Additional Resources
458. Poor Pigs LeetCode Solution image

Problem Statement of Poor Pigs

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. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.
Wait for minutesToDie minutes. You may not feed any other pigs during this time.
After minutesToDie minutes 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.

See also  203. Remove Linked List Elements LeetCode Solution

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.

Constraints:

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

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

458. Poor Pigs LeetCode Solution in C++

class Solution {
 public:
  int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
    const int base = minutesToTest / minutesToDie + 1;
    int ans = 0;
    for (int x = 1; x < buckets; x *= base)
      ++ans;
    return ans;
  }
};
/* code provided by PROGIEZ */

458. Poor Pigs LeetCode Solution in Java

class Solution {
  public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
    final int base = minutesToTest / minutesToDie + 1;
    int ans = 0;
    for (int x = 1; x < buckets; x *= base)
      ++ans;
    return ans;
  }
}
// code provided by PROGIEZ

458. Poor Pigs LeetCode Solution in Python

class Solution:
  def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
    base = minutesToTest // minutesToDie + 1
    ans = 0
    x = 1
    while x < buckets:
      ans += 1
      x *= base
    return ans
# code by PROGIEZ

Additional Resources

See also  427. Construct Quad Tree LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.