Skip to content
Problem 458 · LeetCode 458(opens in a new tab)

Poor Pigs

HardDynamic programming

There are buckets buckets of water, and exactly one of them is poisoned. You find it by letting pigs drink. A pig can drink from as many buckets as you like at once, and many pigs can drink from the same bucket. Drinking takes no time.

After drinking, a pig that had any poison dies exactly minutesToDie minutes later; until then you cannot learn anything or feed anyone. Once that wait is over, the surviving pigs can drink again. You have minutesToTest minutes in total.

Return the fewest pigs that guarantee you can name the poisoned bucket when time runs out.

Example 1

Input:  buckets = 30, minutesToDie = 10, minutesToTest = 20
Output: 4
Why:    2 rounds, so 3 fates per pig; 3³ = 27 < 30 ≤ 81 = 3⁴
2 more examples and the constraints

Example 2

Input:  buckets = 1000, minutesToDie = 10, minutesToTest = 10
Output: 10
Why:    1 round, so 2 fates per pig; 2¹⁰ = 1,024 ≥ 1,000

Example 3

Input:  buckets = 1, minutesToDie = 5, minutesToTest = 50
Output: 0
Why:    a single bucket must be the poisoned one

Constraints

  • 1 ≤ buckets ≤ 1,000
  • 1 ≤ minutesToDie ≤ minutesToTest ≤ 100

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Count outcomes round by roundO(p² · r)
  2. Each pig is a digitO(log buckets)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 1,000 buckets, 1 roundVerdict
Count outcomes round by roundO(p² · r)
Passes; finds the pattern
Each pig is a digitO(log buckets)
Best; one short loop

p = pigs needed, r = rounds = minutesToTest // minutesToDie.

Edge cases to test

  • One bucket. It must be the poisoned one: 0 pigs.
  • Exact powers. 27 buckets with 3 fates need exactly 3 pigs; 28 need 4. Compare with ≥, not >.
  • Time left over. minutesToTest = 100, minutesToDie = 12 gives 8 rounds; the extra 4 minutes are useless. Use integer division.
  • Floating-point logs. 125 buckets with 5 fates: a log formula can return 4; the loop returns 3.

Common mistakes

  • Using 2 fates per pig (dies or lives) even when there is time for several rounds.
  • Forgetting the "never dies" fate and using r instead of r + 1 as the base.
  • Computing the answer with floating-point logarithms and rounding up an exact power.

How to talk through it

  1. 1Clarify. Can a pig drink from many buckets at once? Can a surviving pig be reused in the next round?
  2. 2Solve one round first. Each pig dies or lives, so p pigs give 2^p outcomes: binary codes for buckets.
  3. 3Count the fates. With r rounds a pig dies after round 1, ..., r, or never: r + 1 fates, so base r + 1.
  4. 4Code the loop. Multiply reach by states until it covers buckets; say why it avoids logarithms.
  5. 5Test out loud. Run 1 bucket, an exact power like 27 and the 125 case before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?