LeetCode #887 Hard

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 f for 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.

Constraints
  • 1 <= k <= 100
  • 1 <= n <= 10⁴
dpmathbinary-search
Open on LeetCode ↗
02

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.

How to spot this pattern

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'.

03

Approach

Try it first

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).

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def superEggDrop(self, k, n):
▶3 floors = [0] * (k + 1)
▶4 moves = 0
▶5 while floors[k] < n:
▶6 moves += 1
▶7 for e in range(k, 0, -1):
▶8 floors[e] = floors[e - 1] + floors[e] + 1
▶9 return moves
1 egg2 eggs0 moves001 move2 moves3 movesbuilding16breaksholdsdropcovers++=no moves yet → 0 floors covered
floors[0, 0]1 to 2 eggs
moves0
Each column is an egg count and each cell says how many floors that many eggs can fully check. With zero moves nothing can be checked, so every cell starts at 0. The goal is a 2-egg cell of at least 6.
1 egg2 eggs0 moves001 move2 moves3 movesbuilding16breaksholdsdropcovers++=0 floors < 6 → spend move 1
moves1
floors[2]0goal 6
With 0 moves, 2 eggs can check only 0 of the 6 floors, so one more move is needed. Its row is filled from the previous row only.
1 egg2 eggs0 moves001 move12 moves3 movesbuilding16breaksholdsdropcovers0011++=2 eggs, 1 move → 1 floor
breaks01 eggs, 0 moves
holds02 eggs, 0 moves
floors[2]1
Drop one egg. If it breaks, the answer is below and 1 egg with 0 moves can check 0 floors there. If it holds, 2 eggs with 0 moves can check 0 floors above. Add the drop floor itself: 0 + 0 + 1 = 1. This column is updated before column 1, so its 'breaks' value is still last move's.
1 egg2 eggs0 moves001 move112 moves3 movesbuilding16breaksholdsdropcovers0011++=1 egg, 1 move → 1 floor
breaks00 eggs, 0 moves
holds01 eggs, 0 moves
floors[1]1
Drop one egg. If it breaks, the answer is below and 0 eggs with 0 moves can check 0 floors there (no egg is left, so none). If it holds, 1 egg with 0 moves can check 0 floors above. Add the drop floor itself: 0 + 0 + 1 = 1.
1 egg2 eggs0 moves001 move112 moves3 movesbuilding16breaksholdsdropcovers++=1 floor < 6 → spend move 2
moves2
floors[2]1goal 6
With 1 move, 2 eggs can check only 1 of the 6 floors, so one more move is needed. Its row is filled from the previous row only. The code keeps just one array, so the older rows are greyed: they are shown here only to see where each number came from.
1 egg2 eggs0 moves001 move112 moves33 movesbuilding16breaksholdsdropcovers1113++=2 eggs, 2 moves → 3 floors
breaks11 eggs, 1 moves
holds12 eggs, 1 moves
floors[2]3
Drop one egg. If it breaks, the answer is below and 1 egg with 1 move can check 1 floor there. If it holds, 2 eggs with 1 move can check 1 floor above. Add the drop floor itself: 1 + 1 + 1 = 3. This column is updated before column 1, so its 'breaks' value is still last move's.
1 egg2 eggs0 moves001 move112 moves233 movesbuilding16breaksholdsdropcovers0112++=1 egg, 2 moves → 2 floors
breaks00 eggs, 1 moves
holds11 eggs, 1 moves
floors[1]2
Drop one egg. If it breaks, the answer is below and 0 eggs with 1 move can check 0 floors there (no egg is left, so none). If it holds, 1 egg with 1 move can check 1 floor above. Add the drop floor itself: 0 + 1 + 1 = 2.
1 egg2 eggs0 moves001 move112 moves233 movesbuilding16breaksholdsdropcovers++=3 floors < 6 → spend move 3
moves3
floors[2]3goal 6
With 2 moves, 2 eggs can check only 3 of the 6 floors, so one more move is needed. Its row is filled from the previous row only. The code keeps just one array, so the older rows are greyed: they are shown here only to see where each number came from.
1 egg2 eggs0 moves001 move112 moves233 moves6building16breaksholdsdropcovers2316++=2 eggs, 3 moves → 6 floors
breaks21 eggs, 2 moves
holds32 eggs, 2 moves
floors[2]6
Drop one egg. If it breaks, the answer is below and 1 egg with 2 moves can check 2 floors there. If it holds, 2 eggs with 2 moves can check 3 floors above. Add the drop floor itself: 2 + 3 + 1 = 6. This column is updated before column 1, so its 'breaks' value is still last move's.
1 egg2 eggs0 moves001 move112 moves233 moves36building16breaksholdsdropcovers0213++=1 egg, 3 moves → 3 floors
breaks00 eggs, 2 moves
holds21 eggs, 2 moves
floors[1]3
Drop one egg. If it breaks, the answer is below and 0 eggs with 2 moves can check 0 floors there (no egg is left, so none). If it holds, 1 egg with 2 moves can check 2 floors above. Add the drop floor itself: 0 + 2 + 1 = 3.
1 egg2 eggs0 moves001 move112 moves233 moves36building16breaksholdsdropcovers++=6 ≥ 6 floors → 3 moves
floors[2]6goal 6
answer3
With 3 moves, 2 eggs can check 6 floors, which covers all 6. With 2 moves only 3 were covered, so 3 is the fewest moves that always finds the floor.
05

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.

▶1from functools import cache
▶2 
▶3class Solution:
▶4 def superEggDrop(self, k, n):
▶5 @cache
▶6 def drops(e, f):
▶7 if f == 0 or e == 1:
▶8 return f
▶9 lo, hi = 1, f
▶10 while lo + 1 < hi:
▶11 x = (lo + hi) // 2
▶12 broke, held = drops(e - 1, x - 1), drops(e, f - x)
▶13 if broke < held:
▶14 lo = x
▶15 elif broke > held:
▶16 hi = x
▶17 else:
▶18 lo = hi = x
▶19 return 1 + min(max(drops(e - 1, x - 1), drops(e, f - x)) for x in (lo, hi))
▶20 
▶21 return drops(k, n)
06

Common pitfalls

Trying every floor for the first drop

✗ Wrong
dp[e][f] = 1 + min(max(dp[e - 1][x - 1], dp[e][f - x])
                   for x in range(1, f + 1))
✓ Right
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

✗ Wrong
return ceil(log2(n + 1))
✓ Right
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

✗ Wrong
floors[e] = floors[e - 1] + floors[e]
✓ Right
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.

07

Edge cases

One egg, k = 1

Each 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.

More eggs than you can use

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.

08

Complexity

Time
O(k · m)
Space
O(k)
m is the answer. Each move updates k entries. m is at most n (one egg) and far smaller with more eggs: 141 for k = 2 and n = 10⁴.
09

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.