class Solution:
def twoEggDrop(self, n: int) -> int:
# We need to find the minimal x such that x + (x-1) + ... + 1 >= n
# That is, x(x+1)//2 >= n
x = 0
total = 0
while total < n:
x += 1
total += x
return x
class Solution {
public:
int twoEggDrop(int n) {
int x = 0, total = 0;
while (total < n) {
x++;
total += x;
}
return x;
}
};
class Solution {
public int twoEggDrop(int n) {
int x = 0, total = 0;
while (total < n) {
x++;
total += x;
}
return x;
}
}
var twoEggDrop = function(n) {
let x = 0, total = 0;
while (total < n) {
x++;
total += x;
}
return x;
};
You are given 2
identical eggs and access to a building with n
floors labeled from 1
to n
. Your goal is to determine the minimum number of moves required in the worst case to find the highest floor from which an egg can be dropped without breaking.
In one move, you can drop an egg from any floor. If the egg breaks, you cannot use it again. If it does not break, you can reuse it. You must minimize the number of moves required to guarantee finding the critical floor for any possible scenario.
n
floors.
To solve this problem, let's first consider a brute-force approach: you could drop the first egg from each floor starting from the first, but this could take up to n
moves in the worst case, which is inefficient.
With two eggs, you want to minimize the number of moves required, even in the worst-case scenario. The key insight is to use the first egg to "narrow down" the range where the critical floor is, and then use the second egg to check each floor one by one within that range.
The challenge is to balance the floors you skip with the first egg with the number of remaining moves you might need with the second egg after the first one breaks. This leads to a mathematical approach to minimize the maximum number of moves.
Let's break down the optimal strategy step by step:
x
, then x + (x-1)
, then x + (x-1) + (x-2)
, and so on.k
, you have at most x-1
floors to check with the second egg.x
such that x + (x-1) + ... + 1 ≥ n
.x(x+1)/2 ≥ n
.x
that satisfies the inequality above by incrementing x
and summing until reaching or exceeding n
.x
as the answer.
This approach ensures that, no matter where the critical floor is, you never need more than x
moves.
Let's walk through the process with n = 10
floors:
x
such that x(x+1)/2 ≥ 10
:
x = 1
: 1 < 10x = 2
: 3 < 10x = 3
: 6 < 10x = 4
: 10 = 10 (works!)x = 4
. Drop the first egg from floor 4, then 7 (4+3), then 9 (7+2), then 10 (9+1).
x
such that x(x+1)/2 ≥ n
takes up to √n iterations, which is much faster for large n
.
The space complexity is constant since we only use a few variables.
The two-egg drop problem is a classic example of minimizing the worst-case number of moves using mathematical insight. By realizing that you can use the first egg to "divide and conquer" the search space and the second egg to check sequentially, you reduce the problem to finding the smallest x
where the sum of the first x
numbers is at least n
. This approach ensures efficiency and is much faster than brute-force, especially as n
grows.