You are given two 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.
In 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: n = 2 Output: 2 Explanation: We can drop the first egg from floor 1 and the second egg from floor 2. If the first egg breaks, we know that f = 0. If the second egg breaks but the first egg didn't, we know that f = 1. Otherwise, if both eggs survive, we know that f = 2.
Example 2:
Input: n = 100 Output: 14 Explanation: One optimal strategy is: - Drop the 1st egg at floor 9. If it breaks, we know f is between 0 and 8. Drop the 2nd egg starting from floor 1 and going up one at a time to find f within 8 more drops. Total drops is 1 + 8 = 9. - If the 1st egg does not break, drop the 1st egg again at floor 22. If it breaks, we know f is between 9 and 21. Drop the 2nd egg starting from floor 10 and going up one at a time to find f within 12 more drops. Total drops is 2 + 12 = 14. - If the 1st egg does not break again, follow a similar process dropping the 1st egg from floors 34, 45, 55, 64, 72, 79, 85, 90, 94, 97, 99, and 100. Regardless of the outcome, it takes at most 14 drops to determine f.
Constraints:
1 <= n <= 1000
This problem can be solved efficiently using dynamic programming. The core idea is to break down the problem into smaller subproblems and build up the solution iteratively.
Understanding the Problem
We have two identical eggs and a building with n
floors. We need to find the minimum number of drops required to determine the floor f
where an egg will break when dropped from a floor above f
but will not break when dropped from a floor at or below f
.
Dynamic Programming Approach
Let's define dp[i][j]
as the minimum number of drops needed to find f
given:
i
floorsj
eggsOur goal is to find dp[n][2]
.
Base Cases:
dp[0][j] = 0
(No floors, no drops needed)dp[i][1] = i
(One egg, we must test each floor sequentially)dp[i][j] = infinity
if i < 0
or j < 0
Recursive Relation
If we drop an egg from floor k
(where 1 <= k <= i
), two scenarios are possible:
j-1
eggs and need to test floors 1
to k-1
. The minimum drops for this are dp[k-1][j-1]
.j
eggs and need to test floors k+1
to i
. The minimum drops for this are dp[i-k][j]
.To find the minimum number of drops for the current state, we need to consider the worst-case scenario (the maximum number of drops) from these two possibilities, and add 1 (for the drop we just made):
dp[i][j] = min(1 + max(dp[k-1][j-1], dp[i-k][j]))
for k = 1 to i
Optimization:
Instead of a 2D dp
array, we can optimize to a 1D array since we only use two eggs. dp[i]
represents the minimum drops for i
floors with 2 eggs. The recursive relation simplifies to:
dp[i] = min(1 + max(dp[k-1], dp[i-k]))
for k = 1 to i
Code Implementation (Python)
def twoEggDrop(n):
dp = [float('inf')] * (n + 1) # Initialize dp array
dp[0] = 0 # Base case: 0 floors, 0 drops
dp[1] = 1 # Base case: 1 floor, 1 drop
for i in range(2, n + 1):
for k in range(1, i + 1):
dp[i] = min(dp[i], 1 + max(dp[k - 1], dp[i - k]))
return dp[n]
# Example usage
n = 100
print(twoEggDrop(n)) # Output: 14
Time and Space Complexity:
dp
array of size n+1
.Mathematical Approach (More Efficient):
While dynamic programming works, a more efficient mathematical solution exists. For 2 eggs, the minimum drops needed is approximately the square root of n
. The optimal strategy involves increasing the drop height incrementally. Finding a closed-form solution is beyond the scope of a brief explanation, but this observation indicates an upper bound for the minimum drops. A solution using this approach would have a lower time complexity.