Skip to main content

887. Super Egg Drop

hard

Worst-case minimum drops to find the critical floor using k eggs and n floors. Classic combinatorial DP — invert the recurrence to think in floors-per-moves instead.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

You are given k identical eggs and you have access to a building with n floors labeled from 1 to n. You know that there exists a floor f where 0 <= f <= n such that any egg dropped at a floor higher than f will break, and any egg dropped at or below floor f will not break. Each move, you may take an uncracked egg and drop it from any floor x (where 1 <= x <= n). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves. Return the minimum number of moves that you need to determine with certainty what the value of f is.

Constraints

  • 1 <= k <= 100
  • 1 <= n <= 10^4

Examples

Example 1

Input
k = 1, n = 2
Output
2

Explanation: Drop the egg from floor 1. If it breaks, we know that f = 0. Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1. If it does not break, then we know f = 2. Hence, we need at minimum 2 moves to determine with certainty what the value of f is.

Example 2

Input
k = 2, n = 6
Output
3

Example 3

Input
k = 3, n = 14
Output
4

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Naive: dp[k][n] = 1 + min over x of max(dp[k-1][x-1], dp[k][n-x]). O(k * n^2) — too slow.

Hint 2

Invert: let f(k, m) = max floors solvable with k eggs and m moves. Recurrence f(k, m) = f(k-1, m-1) + f(k, m-1) + 1.

Hint 3

Iterate m upward until f(k, m) >= n. Answer is that m.

Solution approach

Reveal approach

Invert the question. Instead of asking 'min moves for n floors with k eggs', ask 'max floors I can certify with k eggs and m moves' — call this f(k, m). The recurrence is f(k, m) = f(k-1, m-1) + f(k, m-1) + 1: on one move you drop an egg; if it breaks you have k-1 eggs and m-1 moves to certify the floors below; if it survives you have k eggs and m-1 moves to certify the floors above; plus the floor you just tested. Initialize f(*, 0) = 0 and f(0, *) = 0. Increment m starting at 1, computing all f(k', m) for k' from 1 to k, until f(k, m) >= n. Return m. O(k * m) where m is at most about log2(n) * k bounded — comfortably fits the constraints. Space O(k) using rolling rows.

Complexity

Time
O(k * log n)
Space
O(k)

Related patterns

  • dynamic-programming
  • binary-search

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Google
  • Amazon

Practice these live with InterviewChamp.AI

Drill Super Egg Drop and DP 1D problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →