Skip to problem
←

Super Egg Drop

Hard 0 attempts

You have k identical eggs and a building with n floors. There is an unknown highest safe floor f in [0, n]: an egg survives at floors at or below f and breaks above f. A broken egg cannot be reused. Determine the minimum number of drops needed in the worst case to identify f, including the possibilities that every floor is safe or no floor is safe.

Each drop tests one egg from one chosen floor. A surviving egg can be reused, and the next floor may depend on earlier outcomes. Return the guaranteed worst-case drop count, not the unknown floor or an average over outcomes.

Function contract

Implement superEggDrop(k, n).

Output: Return the minimum number of drops sufficient to determine f in the worst case, using at most k eggs.

Example 1

Input
k = 2
n = 6
Output
3

Explanation: Drop first from floor 3. If it breaks, use the remaining egg to check floors 1 and 2. If it survives, test floor 5, then floor 4 or 6 as needed. Three drops always suffice.

Example 2

Input
k = 1
n = 2
Output
2
Constraints
  • 1 <= k <= 100
  • 1 <= n <= 10^4

Count Floors Covered by a Drop Budget

Let covered[e] be the maximum number of floors distinguishable with the current number of moves and e eggs. One more drop splits the possibilities:

  • If the egg breaks, cover the lower floors with one fewer egg.
  • If it survives, cover the higher floors with the same number of eggs.
  • The tested floor contributes one more.

Thus newCovered[e] = oldCovered[e - 1] + oldCovered[e] + 1. Update egg counts downward so both inputs still refer to the previous move budget. Stop when k eggs cover at least n floors.

javascript
function superEggDrop(k, n) {
  const covered = Array(k + 1).fill(0);
  let moves = 0;
  while (covered[k] < n) {
    moves++;
    for (let eggs = k; eggs >= 1; eggs--) {
      covered[eggs] = Math.min(n, covered[eggs - 1] + covered[eggs] + 1);
    }
  }
  return moves;
}

Capping counts at n preserves the feasibility decision and avoids storing irrelevant large coverage values.

Time: O(k × M), where M is the answer and M ≤ n.

Space: O(k). Only the previous move budget is needed.

Loading discussion…

Saved in this browser only. Private to you.

JavaScript