Super Egg Drop
Hard
Topics
You are given a identical eggs and access to a building with b floors. An egg breaks if dropped from a floor at or above some unknown threshold f (0 <= f <= b) and survives below it. Return the minimum number of moves needed to determine f with certainty in the worst case.
Example 1
Input: a = 1, b = 2 Output: 2
Example 2
Input: a = 2, b = 6 Output: 3
Example 3
Input: a = 3, b = 14 Output: 4
Constraints
- 1 <= a <= 100
- 1 <= b <= 10^4
Editorial — intuition, approach, complexity and pitfalls
Intuition
The natural DP indexes on eggs and floors and asks "which floor do I drop from", which is O(eggs · floors²) and needs a clever search to speed up. Inverting the question is far simpler.
Ask instead: with e eggs and m moves available, how many floors can be fully resolved? Call it f(e, m). One drop splits the problem: if the egg breaks, the remaining range is handled by f(e-1, m-1); if it survives, by f(e, m-1). Adding the floor just dropped from gives f(e, m) = f(e-1, m-1) + f(e, m-1) + 1.
That recurrence has no minimisation in it at all, so just increase m until f(eggs, m) reaches the floor count. The shown code keeps one array over eggs and updates it in place from high egg count downward, which is why it reads dp[e] = dp[e] + dp[e-1] + 1.
Approach
- Start
dp[e] = 0for alle, andm = 0. - Increment
m, then updatedp[e] = dp[e] + dp[e-1] + 1foredescending. - Stop when
dp[eggs]reaches the floor count. - Return
m.
Dry Run
eggs = 2, floors = 6
dp[e] = floors fully resolvable with e eggs and m moves
m dp[1] dp[2]
0 0 0
1 1 1
2 2 3
3 3 6 <- dp[2] = 6 reaches the target
answer 3 moves
how the answer scales at 10000 floors
eggs moves
1 10000 one egg forces a linear scan from the bottom
2 141
3 40
5 18
14 14
100 14 past 14 eggs, 2^14 > 10000, so more eggs add nothingComplexity
- Time — O(eggs · m) where
mis the answer. This is worth stating carefully rather than as O(eggs · log floors): with a single egg the answer ism = floors, measured at 10,000 above, so the loop runs 10,000 times. Thelogform only holds onceeggs >= log₂(floors), wheref(e, m)grows like2ᵐandmsaturates — which the table above shows happening at 14 eggs for 10,000 floors. - Space — O(eggs) for the single array.
Common Pitfalls
- Quoting O(eggs · log floors) unconditionally. Measured: one egg needs 10,000 moves at 10,000 floors, so the bound is O(eggs · answer).
- Updating
dp[e]in ascending egg order. The recurrence readsdp[e-1]from the previous move, so the sweep must go downward to avoid overwriting it first. - Reading
f(e, m)as "the highest floor to drop from". It is a count of resolvable floors, which is what makes the recurrence additive and minimisation-free. - Forgetting the
+ 1for the floor actually dropped from, which undercounts and inflates the move count. - Writing the floor-indexed DP with a linear scan over drop positions. That is O(eggs · floors²) — 10⁸ at the stated bounds — and needs either binary search or this inversion to pass.
- Assuming the optimal strategy is binary search over floors. It is not when eggs are scarce: with two eggs the first drop goes to floor 14 of 100, not 50, because a break leaves only a linear scan.
Full Solution — all languages
class Solution:
def superEggDrop(self, a, b):
k, floors = a, b
dp = [0] * (k + 1)
m = 0
while dp[k] < floors:
m += 1
for e in range(k, 0, -1):
dp[e] = dp[e] + dp[e - 1] + 1
return mimport java.util.*;
class Solution {
public int superEggDrop(int a, int b) {
int k = a, floors = b;
int[] dp = new int[k + 1];
int m = 0;
while (dp[k] < floors) {
m++;
for (int e = k; e >= 1; e--) dp[e] = dp[e] + dp[e - 1] + 1;
}
return m;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int superEggDrop(int a, int b) {
int k = a, floors = b;
vector<int> dp(k + 1, 0);
int m = 0;
while (dp[k] < floors) {
m++;
for (int e = k; e >= 1; e--) dp[e] = dp[e] + dp[e - 1] + 1;
}
return m;
}
};/**
* @param {number} a
* @param {number} b
* @return {number}
*/ var superEggDrop = function(a, b) {
const k = a, floors = b;
const dp = new Array(k + 1).fill(0);
let m = 0;
while (dp[k] < floors) {
m++;
for (let e = k; e >= 1; e--) dp[e] = dp[e] + dp[e - 1] + 1;
}
return m;
};