Super Egg Drop

Hard

Topics
MathBinary SearchDynamic Programming

You are given a identical eggs and access to a building with b floors. An egg breaks if dropped from a floor at or above some unknown threshold f (0 <= f <= b) and survives below it. Return the minimum number of moves needed to determine f with certainty in the worst case.

Example 1

21
There is a floor above which an egg always breaks and at or below which it never does, and it is unknown. A broken egg is gone; an unbroken one can be dropped again. The answer is the number of drops that is always enough — the worst case, not the average — with 1 egg and 2 floors, which is 2.
One cell per floor, lowest at the bottom.
Input:  a = 1, b = 2
Output: 2

Example 2

654321
Input:  a = 2, b = 6
Output: 3

Example 3

121110987654321
The figure shows the lowest 12 floors.
Input:  a = 3, b = 14
Output: 4

Constraints

  • 1 <= a <= 100
  • 1 <= b <= 10^4
Editorial — intuition, approach, complexity and pitfalls

Intuition

The natural DP indexes on eggs and floors and asks "which floor do I drop from", which is O(eggs · floors²) and needs a clever search to speed up. Inverting the question is far simpler.

Ask instead: with e eggs and m moves available, how many floors can be fully resolved? Call it f(e, m). One drop splits the problem: if the egg breaks, the remaining range is handled by f(e-1, m-1); if it survives, by f(e, m-1). Adding the floor just dropped from gives f(e, m) = f(e-1, m-1) + f(e, m-1) + 1.

That recurrence has no minimisation in it at all, so just increase m until f(eggs, m) reaches the floor count. The shown code keeps one array over eggs and updates it in place from high egg count downward, which is why it reads dp[e] = dp[e] + dp[e-1] + 1.

Approach

  1. Start dp[e] = 0 for all e, and m = 0.
  2. Increment m, then update dp[e] = dp[e] + dp[e-1] + 1 for e descending.
  3. Stop when dp[eggs] reaches the floor count.
  4. Return m.

Dry Run

eggs = 2, floors = 6

dp[e] = floors fully resolvable with e eggs and m moves

  m   dp[1]  dp[2]
  0   0      0
  1   1      1
  2   2      3
  3   3      6     <- dp[2] = 6 reaches the target

answer 3 moves

how the answer scales at 10000 floors
  eggs    moves
  1       10000      one egg forces a linear scan from the bottom
  2       141
  3       40
  5       18
  14      14
  100     14         past 14 eggs, 2^14 > 10000, so more eggs add nothing

Complexity

  • Time — O(eggs · m) where m is the answer. This is worth stating carefully rather than as O(eggs · log floors): with a single egg the answer is m = floors, measured at 10,000 above, so the loop runs 10,000 times. The log form only holds once eggs >= log₂(floors), where f(e, m) grows like 2ᵐ and m saturates — which the table above shows happening at 14 eggs for 10,000 floors.
  • Space — O(eggs) for the single array.

Common Pitfalls

  • Quoting O(eggs · log floors) unconditionally. Measured: one egg needs 10,000 moves at 10,000 floors, so the bound is O(eggs · answer).
  • Updating dp[e] in ascending egg order. The recurrence reads dp[e-1] from the previous move, so the sweep must go downward to avoid overwriting it first.
  • Reading f(e, m) as "the highest floor to drop from". It is a count of resolvable floors, which is what makes the recurrence additive and minimisation-free.
  • Forgetting the + 1 for the floor actually dropped from, which undercounts and inflates the move count.
  • Writing the floor-indexed DP with a linear scan over drop positions. That is O(eggs · floors²) — 10⁸ at the stated bounds — and needs either binary search or this inversion to pass.
  • Assuming the optimal strategy is binary search over floors. It is not when eggs are scarce: with two eggs the first drop goes to floor 14 of 100, not 50, because a break leaves only a linear scan.

Full Solution — all languages

class Solution:
    def superEggDrop(self, a, b):
        k, floors = a, b
        dp = [0] * (k + 1)
        m = 0
        while dp[k] < floors:
            m += 1
            for e in range(k, 0, -1):
                dp[e] = dp[e] + dp[e - 1] + 1
        return m
SensAI
Beta
Tap the mic to stop voice mode