1884. Egg Drop With 2 Eggs and N Floors
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 7 more drops. Total drops is 1 + 7 = 8.
- 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
Solution
My first idea is to use binary search, if we have infinite eggs. We can start the test from the middle of the floors and then base on the test result to narrow down the searching range.
However, since we only have 2 eggs. The way to search the floor has to be separated into 2 parts.
One is to reduce the range, and the eggs might be broken during the tests.
The goal here is not to find the floor, it is to narrow down the range to figure out a known one
Another part is to use the second(left) eggs to check the known range one by one iteratively
That is what it did in example 2.
So, the next requirement is to guarantee no matter what the floor is, the worst test count is identical.
We have to separate the range in a good manner. The range of the test will be reduced as long as the test is processing. Since we want to reduce the second part of the test (iterate the elements of the range) because we already exhausted some movements in the first part of the tests.
So, Assume the floor height is 100, and that the minimum movement count for this problem is K.
For the last floor 100. When we are testing 100th floor, it shall be the Kth movement.(1 movements is performed) Then for 99, 98, it shall be another range of tests, It shall be K-1th and Kth accordingly. (2 movements is performed) Then for 97, 96, 95, it shall be K-2, k-1, K accordingly. (3 movements is performed) .... For the very first round of test, K, K-1, ... 1, it shall be 1, 2 .... Kth of the test. (K movements is performed) So the problem can be reduced what the K to make (1+ 2+ ....+K) >= 100
This problem can be easily solved by binary search.
public int findMinMovement(int n) {
int lo = 0;
int hi = n;
while(lo < hi) {
int mid = lo + (hi-lo)/2;
if(isOK(mid, n)){
mid = hi;
}else{
lo = mid+1;
}
}
}
public boolean isOK(int num, int n) {
return num*(num+1)/2 >= n;
}
Last updated
Was this helpful?