You have two identical eggs and a building with 100 floors. There's some floor at or below which an egg survives a drop, and above which it breaks ( can be 0, if every drop breaks it, or 100, if none do). An egg that survives can be dropped again. A broken one is gone.
What's the fewest drops that guarantees you find , however unlucky you are?
"However unlucky" is the whole problem. You're not minimizing the average, you're minimizing the worst case, and the adversary picks after seeing your strategy.
Why the obvious strategies lose
With one egg there's only one safe strategy: start at floor 1 and go up one at a time. The first break tells you . Skip a floor and a break leaves you unable to tell which of the two floors did it. So one egg costs up to 100 drops.
The second egg is what buys you speed: you can use the first one to take big jumps, and fall back to the careful floor-by-floor walk with the second once the first breaks.
| Strategy for the first egg | Drops at | Worst case |
|---|---|---|
| None (one egg, walk up) | 1, 2, 3, ... | 100 |
| Binary search | 50, then 75 if it survives | 50 |
| Fixed steps of 10 | 10, 20, ..., 100 | 19 |
| Shrinking steps | 14, 27, 39, 50, ... | 14 |
Binary search is the instinct, and it's the worst of the jumping strategies. Drop at 50 and it breaks: you have one egg left and 49 floors below, which is 49 more drops, one at a time. Binary search assumes a failed probe is cheap. Here the first failure costs you half your tools.
Steps of 10 do much better. The worst case is when the first egg survives nine times and breaks at 100: that's 10 drops, then 9 more walking 91 to 99. But notice the imbalance. If it breaks on the very first drop, at 10, you only need 9 more: 10 total. Fixed steps make late breaks expensive and early breaks cheap. The worst case is set by the expensive end.
Balance the worst case
Plot the total cost against which drop breaks the first egg, and the problem with fixed steps is obvious:
Every time the first egg survives, you've spent a drop. So the next gap should be one floor shorter, to keep the total the same whichever drop it breaks on.
Say the answer is drops. The first drop goes at floor : if it breaks, the second egg walks floors 1 to , drops total. If it survives, you have drops left, so the next drop goes floors higher, then , and so on down to 1. The floors you can cover add up to
(1) Floors covered with d drops and two eggs must reach n.
For : falls short, clears it. So 14 drops, with the first egg going at 14, 27, 39, 50, 60, 69, 77, 84, 90, 95, 99 and 100.
Check it at a couple of points. Break at 14: walk 1 to 13, that's . Break at 27: walk 15 to 26, that's . Every break costs the same total. That's what balanced means.
It's also a proof that 13 can't work. With 13 drops the first one has to be at floor 13 or lower (a break there leaves 12 drops for the floors below). The next can be at most 12 floors higher, and so on. The furthest you can reach is , and a building with 100 floors has nine you'd never test.
Flip the question
"Fewest drops for floors" is awkward to compute directly. The inverse is easy: with drops and eggs, how many floors can you fully cover? Call it .
Make one drop, from some floor. Either the egg breaks, and you have drops and eggs for the floors below; or it survives, and you have drops and eggs for the floors above. Plus the floor you just tested:
(2) Floors coverable with d drops and k eggs.
with (no drops, no floors) and (no eggs, no floors).
With one egg, : the walk up. With two, the recurrence gives , which unrolls to , exactly equation (1). The general answer is a sum of binomial coefficients:
(3) Closed form for any number of eggs.
It follows from equation (2) by Pascal's rule, , with the supplying the term.
Give yourself as many eggs as drops () and equation (3) becomes .
Counting floors, one drop at a time
- 1
Keep
covered[k], the floors checkable with the drops so far and eggs. - 2
Add one drop and update every
covered[k]with equation (2). - 3
Stop as soon as
covered[eggs]reaches the number of floors.
For two eggs specifically you can skip the loop and solve equation (1) for directly: , which for is .
time, where is the answer (about for two eggs, about once eggs are plentiful), and space.
The textbook DP instead tries every floor for the next drop, . It's correct and , too slow for Super Egg Drop's . Flipping the question to "floors per number of drops" is what makes it fast, and it's the move worth keeping: when the direct question is hard, ask how much a given budget can buy.