Super Egg Drop (LeetCode 887): Solution and Explanation

Hard
Math
Binary Search
Dynamic Programming

You have k identical eggs and a building with n floors. Some unknown floor f separates floors where eggs survive from floors where they break. Find the minimum number of drops that guarantees you can identify f in the worst case.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

The textbook DP defines T(k, n) as the worst case moves needed with k eggs and n floors. Dropping from floor x either breaks the egg, leaving T(k - 1, x - 1), or does not, leaving T(k, n - x), and we take the best x of the worst branch. That is O(k × n²), or O(k × n log n) with binary search on x, which is still heavy for n up to 10^4 and awkward to get right.

The breakthrough is to turn the question around. Instead of asking how many moves n floors need, ask how many floors we can fully resolve with m moves and j eggs. Call it dp[m][j]. Make one drop from the best floor. If the egg breaks, we have m - 1 moves and j - 1 eggs left, which can resolve dp[m - 1][j - 1] floors below. If it survives, we have m - 1 moves and j eggs, which resolve dp[m - 1][j] floors above. Plus the floor we just tested. So dp[m][j] = dp[m - 1][j - 1] + dp[m - 1][j] + 1.

There is no minimization inside this recurrence, which is what makes it fast. We simply increase m one move at a time and fill in the row for every egg count until dp[m][k] reaches n. The first m that covers all n floors is the answer. Because coverage grows very quickly when there are several eggs, m is often far smaller than n.

Approach: DP on moves and eggs (floors covered)

  1. Allocate dp with n + 1 rows (moves) and k + 1 columns (eggs), all zero. Zero moves or zero eggs cover zero floors.
  2. Set m = 0 and loop while dp[m][k] < n.
  3. Increase m by one.
  4. For each egg count j from 1 to k, set dp[m][j] = dp[m - 1][j - 1] + dp[m - 1][j] + 1.
  5. When dp[m][k] reaches at least n, return m. The loop always stops by m = n, since one egg covers m floors with m moves.

Step-by-step example

k = 2, n = 6
  1. m = 1: dp[1][1] = 0 + 0 + 1 = 1 and dp[1][2] = 0 + 0 + 1 = 1. One drop resolves one floor. 1 is less than 6, so continue.
  2. m = 2: dp[2][1] = dp[1][0] + dp[1][1] + 1 = 2 and dp[2][2] = dp[1][1] + dp[1][2] + 1 = 3. Still less than 6.
  3. m = 3: dp[3][1] = 0 + 2 + 1 = 3 and dp[3][2] = dp[2][1] + dp[2][2] + 1 = 2 + 3 + 1 = 6.
  4. dp[3][2] = 6 reaches n, so the answer is 3. For example, drop first from floor 3 and then adjust based on whether the egg broke.

Code: Python, Java, C++

1class Solution:
2    def superEggDrop(self, k: int, n: int) -> int:
3        # dp[m][k] = max floors we can check with m moves and k eggs
4        # We want minimum m such that dp[m][k] >= n
5
6        dp = [[0] * (k + 1) for _ in range(n + 1)]
7
8        m = 0
9        while dp[m][k] < n:
10            m += 1
11            for j in range(1, k + 1):
12                # If egg breaks: dp[m-1][j-1] floors below
13                # If egg survives: dp[m-1][j] floors above
14                # Plus current floor = 1
15                dp[m][j] = dp[m - 1][j - 1] + dp[m - 1][j] + 1
16
17        return m

Time and space complexity

Time

O(k × n)

The loop runs m times with k work each, where m is the answer and never exceeds n, and allocating the table also costs O(k × n). With several eggs m is close to log n, so the loop itself is usually much cheaper.

Space

O(k × n)

The reference code allocates an (n + 1) × (k + 1) table. Only the previous row is ever read, so it could be reduced to O(k).

Other approaches

Classic DP with binary search on the drop floor (time O(k × n log n), space O(k × n))

Compute T(k, n) as the minimum over x of 1 + max(T(k - 1, x - 1), T(k, n - x)). The break branch grows with x and the survive branch shrinks, so binary search finds the best x. It is the more familiar formulation and passes, but it is slower and easier to get wrong.

Common mistakes

  • Writing the classic recurrence with a linear scan over every drop floor, which is O(k × n²) and times out.
  • Forgetting the + 1 for the floor being tested in the coverage recurrence.
  • Swapping the branches: a broken egg loses an egg and explores below, a surviving egg keeps all eggs and explores above.
  • Thinking binary search on floors is optimal. With limited eggs, halving the building wastes eggs, which is why the answer for 2 eggs and 100 floors is 14, not 7.

Frequently asked questions

Why is the inverted DP faster than the classic egg drop DP?

The classic version must choose the best floor at every state, adding a minimization. Counting floors covered by m moves has a direct sum recurrence with no choice inside, so each state costs O(1).

What is the answer for 2 eggs and 100 floors?

14. With 2 eggs, m moves cover m(m + 1) / 2 floors, and 14 is the smallest m where that reaches 100.

Is there a math formula for Super Egg Drop?

Yes. The number of floors covered by m moves and k eggs equals the sum of the binomial coefficients C(m, 1) through C(m, k). You can binary search m with that formula, which gives the same answer as the DP.