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. Each pig can feed from any number of buckets, and each bucket can be fed from by any number of pigs.
  4. Wait for minutesToDie minutes. You may not feed any other pigs during this time.
  5. After minutesToDie minutes have passed, any pigs that have been fed the poisonous bucket will die, and all others will survive.
  6. 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.

Input & Output

Example 1 — Basic Case
$ Input: buckets = 4, minutesToDie = 15, minutesToTest = 15
Output: 2
💡 Note: We have 15/15 = 1 test round. Each pig has 2 states (die or live). With 2 pigs: 2² = 4 combinations, exactly enough for 4 buckets.
Example 2 — Multiple Test Rounds
$ Input: buckets = 4, minutesToDie = 15, minutesToTest = 30
Output: 2
💡 Note: We have 30/15 = 2 test rounds. Each pig has 3 states (die test 1, die test 2, or never die). With 1 pig: 3¹ = 3 < 4, so we need 2 pigs: 3² = 9 >= 4.
Example 3 — Single Bucket
$ Input: buckets = 1, minutesToDie = 1, minutesToTest = 1
Output: 0
💡 Note: Only one bucket, so it must be poisonous. No pigs needed to test.

Constraints

  • 1 ≤ buckets ≤ 1000
  • 1 ≤ minutesToDie ≤ minutesToTest ≤ 100

Visualization

Tap to expand
Poor Pigs - Finding Poisonous Bucket INPUT 4 Buckets (1 is poisonous) 1 2 3 4 (poison) Pigs to test: Pig A Pig B buckets = 4 minutesToDie = 15 minutesToTest = 15 rounds = 15/15 = 1 ALGORITHM STEPS 1 Calculate Rounds rounds = test/die = 1 2 States per Pig states = rounds + 1 = 2 3 Find Min Pigs states^pigs >= buckets 4 Solve Equation 2^pigs >= 4 2 Pigs = 4 States 00 01 10 11 A:alive B:alive 0=alive 1=dead FINAL RESULT Testing Strategy: A feeds 3,4 B feeds 2,4 After 15 mins: Both alive: Bucket 1 Only A dead: Bucket 3 Only B dead: Bucket 2 Both dead: Bucket 4 OK OUTPUT 2 pigs needed Key Insight: Each pig can be in (rounds + 1) states: die in round 1, round 2, ... or survive all rounds. With P pigs and S states each, we can distinguish S^P different buckets. Formula: pigs = ceil(log(buckets) / log(rounds + 1)) = ceil(log(4) / log(2)) = 2 TutorialsPoint - Poor Pigs | Optimal Solution
Asked in
Google 15 Facebook 10 Amazon 8
28.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen