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;
};
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.
n
).
Constraints:
1 ≤ k ≤ 100
1 ≤ n ≤ 10^4
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.
dp[m][k]
as the maximum number of floors that can be checked with k
eggs and m
moves.
k-1
eggs and m-1
moves left.k
eggs and m-1
moves left.dp[m][k] = 1 + dp[m-1][k-1] + dp[m-1][k]
m
such that dp[m][k] ≥ n
.
This approach is much more efficient than simulating every possible drop or using naive recursion.
Suppose you have k = 2
eggs and n = 6
floors.
dp[2][1] = 2
, dp[2][2] = 3
dp[3][2] = 6
This demonstrates how the DP relationship builds up to the solution.
n
.
k
eggs.n
moves (but usually much less).m
is the answer (at most log n for practical inputs).This makes the optimized solution feasible for the largest constraints.
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.