I was just reading The Two Egg Problem:
The Two Egg Problem
You are given two eggs, and access to a 100-storey building. Both eggs are identical. The aim is to find out the highest floor from which an egg will not break when dropped out of a window from that floor. If an egg is dropped and does not break, it is undamaged and can be dropped again. However, once an egg is broken, that’s it for that egg.
If an egg breaks when dropped from floor
n, then it would also have broken from any floor above that. If an egg survives a fall, then it will survive any fall shorter than that.The question is: What strategy should you adopt to minimize the number egg drops it takes to find the solution?. (And what is the worst case for the number of drops it will take?)
I was following along until the "Look see I can do three" section. The author states that after the first egg breaks it degrades into the 2-egg problem and can be solved recursively.
That’s great, but wouldn’t we want to choose larger step-sizes when using 3 eggs instead of 2 (for the first egg)? From which floor do we throw the first egg?
With 1 egg, we have to start at floor 1.
With 2 eggs, we solve for n(n+1)/2=k and round up, where n is the starting floor, and k is the number of floors.
With 3… I’m having trouble coming up with a formula.
Thinking about this a bit more, with 2 eggs, the maximum number of drops is equal to the floor number that we drop our first egg from. For example, with 2 eggs and 100 floors, the solution is 14, which means we drop the first egg from floor 14, and if it breaks, we have to drop up to 13 more times, for floors 1-13.
With 3 eggs, the solution is 9 (as shown in the chart). But we wouldn’t want to throw the first egg at floor 9, we can throw it higher, because we don’t have to iterate by 1s in-between.
If we throw from floor 14 again, and it breaks, then we recurse. n(n+1)/2=k where k is now 13… but that gives us 4.815, if we ceil and that and add our previous drop we get 6, which is lower than the actual solution, so something here is wrong…
What if it doesn’t break? Then you have a three-egg problem with 86 floors, which takes maybe one drop less to solve than the 100-floor problem.
Say you drop the first egg from the 50th floor. If it breaks, you have a two-egg problem with 49 floors, which takes up to 10 additional drops. So that would give you a worst-case of 11 drops (since if it doesn’t break, the 50-floor three-egg problem takes at most 7 additional drops).
If you choose the 37th floor for the first drop, if it breaks, you have a 36-floor two-egg problem, needing up to 8 additional drops. If it doesn’t break, you have a 63-floor three-egg problem left. You want to solve that problem with at most 8 drops, so if the next drop breaks the egg, the remaining two-egg problem should be solvable in at most 7 drops, thus the highest floor you can choose for the second drop is
37 + 28 + 1 = 66, since 28 floors is the highest you can solve with at most 7 drops and two eggs. If the egg doesn’t break, you have a 34-floor three-egg problem with 7 drops left. The highest you can certainly solve with the remaining 6 drops if the egg breaks is 21 (6*7/2), so you can choose floor66 + 21 + 1 = 88. If the egg doesn’t break, you have 12 floors left with 6 drops, which is already doable with only two eggs.Systematically, the highest number of floors you can certainly solve with
ddrops andeeggs isIf you have only one drop, you have no choice but to choose the lowest floor of which you do not yet know that the egg doesn’t break. If that breaks it, and you tried a higher floor, you don’t know the first floor to break the egg.
If you have only one egg, you have to check every floor in order until the egg breaks or you run out of drops.
Otherwise, if the first drop is from a floor higher than
F(e-1,d-1) + 1, you possibly can’t find the first breaking floor if the egg breaks. If the first drop is from a lower floor, you can’t reach as high withd-1drops if the egg doesn’t break, so the first drop should be from floorF(e-1,d-1) + 1. If it breaks, you can solve with the remaininge-1eggs andd-1drops by assumption. If not, you can solve for the nextF(e,d-1)floors with the remaining drops and eggs.Conversely, to find how many drops you may need for
ffloors witheeggs, you have to findYou can find that by calculating the
F(e,d)matrix, or you can use dynamic programming:If you choose floor
sfor the first drop, if the egg breaks, you need up toD(e-1,s-1)drops to determine the floor. If the egg doesn’t break, you need up toD(e,f-s)drops to determine the floor.So the worst case for choosing floor
sfor the first drop isand the best of the worst case is
(where of course
D(e,0) = 0).