Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

887. Super Egg Drop - Leetcode Solution

Code Implementation

class Solution:
    def superEggDrop(self, k: int, n: int) -> int:
        # dp[m][k]: max floors that can be checked with k eggs and m moves
        dp = [0] * (k + 1)
        m = 0
        while dp[k] < n:
            m += 1
            for i in range(k, 0, -1):
                dp[i] = dp[i] + dp[i-1] + 1
        return m
      
class Solution {
public:
    int superEggDrop(int k, int n) {
        vector<int> dp(k + 1, 0);
        int m = 0;
        while (dp[k] < n) {
            m++;
            for (int i = k; i > 0; --i) {
                dp[i] = dp[i] + dp[i-1] + 1;
            }
        }
        return m;
    }
};
      
class Solution {
    public int superEggDrop(int k, int n) {
        int[] dp = new int[k + 1];
        int m = 0;
        while (dp[k] < n) {
            m++;
            for (int i = k; i > 0; --i) {
                dp[i] = dp[i] + dp[i-1] + 1;
            }
        }
        return m;
    }
}
      
var superEggDrop = function(k, n) {
    let dp = new Array(k + 1).fill(0);
    let m = 0;
    while (dp[k] < n) {
        m++;
        for (let i = k; i > 0; --i) {
            dp[i] = dp[i] + dp[i-1] + 1;
        }
    }
    return m;
};
      

Problem Description

You are given k eggs and a building with n floors. Your task is to determine the minimum number of moves required to find out the highest floor (F) from which an egg can be dropped without breaking, assuming all eggs are identical and will break if dropped from above floor F and will not break if dropped from F or below.

  • You must find F with certainty, regardless of how the eggs break.
  • Each move, you may drop an egg from any floor (1 to n).
  • If the egg breaks, it cannot be used again.
  • If the egg does not break, it can be reused.
  • Your goal: minimize the number of moves required in the worst case.

Constraints:
1 ≤ k ≤ 100
1 ≤ n ≤ 10^4

Thought Process

The problem is a classic example of optimizing for the worst-case scenario with limited resources. The first instinct might be to try every floor one by one (linear search), but with many floors and few eggs, this is inefficient. Alternatively, binary search works only if you have unlimited eggs, which is not the case here.

The challenge is to balance the number of eggs and the number of moves, ensuring that you never run out of eggs before you're certain of the answer. This requires a dynamic programming approach that considers both the number of eggs left and the number of moves made.

The key insight is to analyze the problem by moves: How many floors can I check with k eggs in m moves? If we can check at least n floors within m moves, then m is a possible answer.

Solution Approach

  • Dynamic Programming by Moves: Define dp[m][k] as the maximum number of floors that can be checked with k eggs and m moves.
  • Recurrence Relation: At each move, you can drop an egg:
    • If it breaks: you have k-1 eggs and m-1 moves left.
    • If it doesn't break: you have k eggs and m-1 moves left.
    So,
    dp[m][k] = 1 + dp[m-1][k-1] + dp[m-1][k]
  • Goal: Find the minimal m such that dp[m][k] ≥ n.
  • Implementation: Instead of a 2D array, we can optimize to a 1D array since each state depends only on the previous move.
  • Why This Works: Each move gives you the ability to partition the problem, narrowing down the possible floor range no matter the outcome of the egg drop.

This approach is much more efficient than simulating every possible drop or using naive recursion.

Example Walkthrough

Suppose you have k = 2 eggs and n = 6 floors.

  1. First Move: Drop from floor 3 (middle). If it breaks, you have 1 egg left and need to check floors 1-2 (2 moves). If it doesn't, check floors 4-6 (next moves).
  2. Using the DP Table:
    • After 1 move: can only check 1 floor regardless of eggs.
    • After 2 moves: dp[2][1] = 2, dp[2][2] = 3
    • After 3 moves: dp[3][2] = 6
  3. Result: With 2 eggs and 3 moves, you can check all 6 floors. Therefore, the answer is 3 moves.

This demonstrates how the DP relationship builds up to the solution.

Time and Space Complexity

  • Brute-force approach: O(n^k) or O(n*k) with recursion and memoization, which is infeasible for large n.
  • Optimized DP approach:
    • Each move, we update up to k eggs.
    • Worst-case, we iterate up to n moves (but usually much less).
    • Total time: O(k * m), where m is the answer (at most log n for practical inputs).
    • Space: O(k) for the DP array.

This makes the optimized solution feasible for the largest constraints.

Summary

The Super Egg Drop problem is about minimizing the number of moves to determine the critical floor with a limited number of eggs. Instead of simulating every possible drop, we use dynamic programming to track how many floors can be checked with a given number of moves and eggs. The key insight is to flip the problem: for a given number of moves, how many floors can we check? This leads to an efficient O(k log n) solution that is practical even for large inputs, making the approach both elegant and powerful.