Super Egg Drop Problem
Description
LeetCode Problem 887.
You are given k identical eggs and you have access to a building with n floors labeled from 1 to n.
You know that there exists a floor f where 0 <= f <= n such that any egg dropped at a floor higher than f will break, and any egg dropped at or below floor f will not break.
Each move, you may take an unbroken egg and drop it from any floor x (where 1 <= x <= n). If the egg breaks, you can no longer use it. However, if the egg does not break, you may reuse it in future moves.
Return the minimum number of moves that you need to determine with certainty what the value of f is.
Example 1:
Input: k = 1, n = 2
Output: 2
Explanation:
Drop the egg from floor 1. If it breaks, we know that f = 0.
Otherwise, drop the egg from floor 2. If it breaks, we know that f = 1.
If it does not break, then we know f = 2.
Hence, we need at minimum 2 moves to determine with certainty what the value of f is.
Example 2:
Input: k = 2, n = 6
Output: 3
Example 3:
Input: k = 3, n = 14
Output: 4
Constraints:
- 1 <= k <= 100
- 1 <= n <= 10^4
Solution
Approach: DP on Moves Instead of Floors
The direct DP, "fewest moves for k eggs and n floors", tries every drop floor in every state and is O(k · n2), too slow for n up to 104. Turning the question around makes it easy:
With m moves and e eggs, what is the largest number of floors we can fully check? Call it floors(m, e).
Make the first drop from some floor. One of two things happens:
- It breaks. We have
m - 1moves ande - 1eggs left, and must check the floors below: at mostfloors(m - 1, e - 1)of them. - It does not break. We have
m - 1moves and stilleeggs, for the floors above: at mostfloors(m - 1, e).
Choosing the drop floor so that exactly floors(m - 1, e - 1) floors lie below it, we cover those, the floor we dropped from, and the ones above:
floors(m, e) = floors(m - 1, e - 1) + 1 + floors(m - 1, e), with floors(0, e) = floors(m, 0) = 0.
The answer is the smallest m with floors(m, k) >= n. Keep one array floors[0..k] and, for each new move, update it for e from k down to 1. Going down matters: floors[e - 1] must still hold the value for m - 1 moves when floors[e] is updated.
Example: with 2 eggs, floors for m = 1, 2, 3 is 1, 3, 6, so 6 floors need 3 moves (Example 2).
The number of moves never exceeds n (one egg, one floor at a time), and with two or more eggs it is O(√n). The values stay at most about 2n + 1, so 32-bit integers are enough.
Complexity: O(k · m) time, where m is the answer (at most n, and O(√n) once k >= 2), and O(k) space.
class Solution:
def superEggDrop(self, k: int, n: int) -> int:
# floors[e]: the most floors we can fully check with e eggs and the moves made so far.
floors = [0] * (k + 1)
moves = 0
while floors[k] < n:
moves += 1
# Go down in e so floors[e - 1] still holds the value for one move fewer.
for e in range(k, 0, -1):
# Drop once: if it breaks, check floors[e - 1] below; if not, floors[e] above.
floors[e] = floors[e - 1] + floors[e] + 1
return moves
Now write it from memory
Close the write-up and solve it yourself against the tests.