Super Egg Drop

hard dynamic programming egg drop math

Problem

You have k identical eggs and a building with n floors. There is a critical floor f (0 ≤ f ≤ n): an egg dropped from a floor above f breaks, and an egg dropped at or below f does not. You may reuse an egg that did not break. Return the minimum number of moves needed to determine f with certainty in the worst case.

Inputk = 2, n = 6
Output3
With 2 eggs, 3 moves can certainly distinguish among 7 outcomes (floors 0..6), so 3 moves suffice for 6 floors.

def super_egg_drop(k, n):
    dp = [0] * (k + 1)
    moves = 0
    while dp[k] < n:
        moves += 1
        for e in range(k, 0, -1):
            dp[e] = dp[e] + dp[e - 1] + 1
    return moves
function superEggDrop(k, n) {
  const dp = new Array(k + 1).fill(0);
  let moves = 0;
  while (dp[k] < n) {
    moves++;
    for (let e = k; e >= 1; e--) {
      dp[e] = dp[e] + dp[e - 1] + 1;
    }
  }
  return moves;
}
class Solution {
    public int superEggDrop(int k, int n) {
        int[] dp = new int[k + 1];
        int moves = 0;
        while (dp[k] < n) {
            moves++;
            for (int e = k; e >= 1; e--) {
                dp[e] = dp[e] + dp[e - 1] + 1;
            }
        }
        return moves;
    }
}
int superEggDrop(int k, int n) {
    vector<int> dp(k + 1, 0);
    int moves = 0;
    while (dp[k] < n) {
        moves++;
        for (int e = k; e >= 1; e--) {
            dp[e] = dp[e] + dp[e - 1] + 1;
        }
    }
    return moves;
}
Time: O(k · √n) Space: O(k)