KatInterview

923. Super Egg Drop

HardSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method Explanation

This problem asks for the minimum number of moves to determine a critical floor f with certainty. This is a classic dynamic programming problem. We can approach it from two different perspectives.

Solution

Super Egg Drop in Python

class Solution:
    def superEggDrop(self, k: int, n: int) -> int:
        # dp[j] will store the maximum number of floors that can be checked
        # with the current number of moves and j eggs.
        dp = [0] * (k + 1)
        moves = 0
        # We continue to make moves as long as the max floors we can check
        # with k eggs is less than n.
        while dp[k] < n:
            moves += 1
            # Update dp table for the current number of moves.
            # We iterate from k down to 1 to use the results from the previous move (moves-1).
            # The recurrence is: dp_moves[j] = dp_{moves-1}[j] + dp_{moves-1}[j-1] + 1
            for j in range(k, 0, -1):
                dp[j] = dp[j] + dp[j - 1] + 1
        return moves

Super Egg Drop in Java

class Solution {
    public int superEggDrop(int k, int n) {
        // dp[j] will store the maximum number of floors that can be checked
        // with the current number of moves and j eggs.
        int[] dp = new int[k + 1];
        int moves = 0;
        // We continue to make moves as long as the max floors we can check
        // with k eggs is less than n.
        while (dp[k] < n) {
            moves++;
            // Update dp table for the current number of moves.
            // We iterate from k down to 1 to use the results from the previous move (moves-1).
            // The recurrence is: dp_moves[j] = dp_{moves-1}[j] + dp_{moves-1}[j-1] + 1
            for (int j = k; j > 0; j--) {
                dp[j] = dp[j] + dp[j - 1] + 1;
            }
        }
        return moves;
    }
}

Super Egg Drop in C++

class Solution {
public:
    int superEggDrop(int k, int n) {
        // dp[j] will store the maximum number of floors that can be checked
        // with the current number of moves and j eggs.
        std::vector<int> dp(k + 1, 0);
        int moves = 0;
        // We continue to make moves as long as the max floors we can check
        // with k eggs is less than n.
        while (dp[k] < n) {
            moves++;
            // Update dp table for the current number of moves.
            // We iterate from k down to 1 to use the results from the previous move (moves-1).
            // The recurrence is: dp_moves[j] = dp_{moves-1}[j] + dp_{moves-1}[j-1] + 1
            for (int j = k; j > 0; --j) {
                dp[j] = dp[j] + dp[j - 1] + 1;
            }
        }
        return moves;
    }
};

Super Egg Drop in TypeScript

function superEggDrop(k: number, n: number): number {
    // dp[j] will store the maximum number of floors that can be checked
    // with the current number of moves and j eggs.
    const dp: number[] = new Array(k + 1).fill(0);
    let moves = 0;
    // We continue to make moves as long as the max floors we can check
    // with k eggs is less than n.
    while (dp[k] < n) {
        moves++;
        // Update dp table for the current number of moves.
        // We iterate from k down to 1 to use the results from the previous move (moves-1).
        // The recurrence is: dp_moves[j] = dp_{moves-1}[j] + dp_{moves-1}[j-1] + 1
        for (let j = k; j > 0; j--) {
            dp[j] = dp[j] + dp[j - 1] + 1;
        }
    }
    return moves;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.