Super Egg Drop
Super Egg Drop is LeetCode 887 (Hard). You have k identical eggs and a building with floors 1 to n. Some floor f between 0 and n is the breaking point: an egg dropped from above f breaks, and one dropped from f or lower survives.
- An egg that survives can be dropped again; a broken egg is gone.
- Return the smallest number of moves that finds
ffor certain, whatever floor it turns out to be.
With n up to 10,000 and k up to 100, the classic table that tries every floor for every state is far too slow.
- 1 <= k <= 100
- 1 <= n <= 10⁴
Intuition
The textbook form of the egg dropping puzzle asks how many moves n floors need, and answering it means trying every floor as the first drop. Turn the question around: with m moves and e eggs, how many floors can you fully check? Call that cover(m, e).
Drop from some floor. If the egg breaks, the answer is below, and with m − 1 moves and e − 1 eggs you can check cover(m − 1, e − 1) floors there. If it survives, cover(m − 1, e) floors above can be checked. Adding the floor you dropped from:
cover(m, e) = cover(m − 1, e − 1) + cover(m − 1, e) + 1
No choice is left to optimise, so just raise m until cover(m, k) ≥ n.
When a DP has to try every choice for each state, check whether swapping the answer with one of the inputs removes the choice. Here 'moves needed for n floors' becomes 'floors checked by m moves'.
Approach
Before reading on, suppose you have 2 eggs and 3 moves. What is the tallest building you can always solve? Work out where the first drop must go. Aim for O(k · answer).
Two ways to solve it
Count how many floors m moves can check, and add moves until that reaches n.
- No search: each entry is a plain sum.
- Tiny memory: one array of k + 1 numbers.
The shortest and fastest answer.
Memoize the minimum moves for e eggs and f floors, and binary search the first drop floor.
- Familiar: the textbook recurrence, made fast enough.
- Why search works: the break side grows and the survive side shrinks as the drop floor rises.
Pick it if the interviewer starts from the classic table.
Both pass; the coverage loop wins on time, memory and code length. The steps, code and live demo follow the coverage loop, and the binary search version comes after the demo.
Keep one coverage number per egg count
floors[e] holds how many floors e eggs can check with the moves spent so far. All entries start at 0: with no moves, no floor can be checked, however many eggs you hold.
Spend one more move
Add 1 to moves, then set floors[e] = floors[e − 1] + floors[e] + 1 for every egg count. One extra move covers the floors below the drop, the floors above it and the drop floor itself.
Update from the most eggs down
Run e from k down to 1. floors[e] must read floors[e − 1] from the previous move, and going downward reads it before it is overwritten, so one array is enough.
Stop when the building is covered
As soon as floors[k] ≥ n, return moves. Coverage grows quickly (2 eggs and m moves cover m(m + 1)/2 floors), so 10,000 floors need only 141 moves with 2 eggs and 14 with 100 eggs.
Code in Python, C++ and Java
Memoized moves with binary search
drops(e, f) is the fewest moves for e eggs and f floors. Dropping from floor x costs 1 + max(drops(e − 1, x − 1), drops(e, f − x)). The first term rises with x and the second falls, so binary search finds where they cross instead of trying every floor.
Common pitfalls
Trying every floor for the first drop
dp[e][f] = 1 + min(max(dp[e - 1][x - 1], dp[e][f - x])
for x in range(1, f + 1))floors[e] = floors[e - 1] + floors[e] + 1
The minimax table is correct but costs O(k · n²): about 10¹⁰ steps for k = 100, n = 10⁴. The coverage form has no inner loop over floors.
Answering with plain binary search
return ceil(log2(n + 1))
loop until floors[k] >= n, then return moves
Halving works only with enough eggs. With k = 1, n = 3, dropping from floor 2 first and breaking leaves floor 1 unknown and no egg to test it. One egg must climb floor by floor, so the answer is 3, not 2.
Leaving out the drop floor
floors[e] = floors[e - 1] + floors[e]
floors[e] = floors[e - 1] + floors[e] + 1
The floor you drop from is decided by that drop, so it adds 1. Without it every entry stays 0 and the loop never ends, on any input.
Edge cases
k = 1Each move adds exactly one floor to floors[1], so the loop returns n: the only safe plan is to test floor 1, then 2, and so on.
Once k ≥ ⌈log₂(n + 1)⌉, the answer equals ⌈log₂(n + 1)⌉, the binary search count. For n = 10⁴ that is 14, whether k is 14 or 100.
Complexity
Super Egg Drop FAQ
With 2 eggs and 100 floors, where do you drop?
14 moves are enough, since 14 moves cover 14 × 15 / 2 = 105 floors. Drop the first egg from floor 14: if it breaks, floors 1 to 13 take at most 13 more drops with the last egg. If it survives, go up 13 floors to 27, then 39, 50, 60, 69, 77, 84, 90, 95, 99 and 100. Each jump is one floor shorter because one move is already spent.
Is there a formula instead of a loop?
Yes. cover(m, k) equals the sum of the binomial coefficients C(m, 1) + C(m, 2) + … + C(m, k). Binary search on m with that sum gives O(k log n) time, though the loop is already fast enough.