I have been looking at the following coding problem.
There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. If it's dropped from any floor below, it will not break. You're given two eggs. Find N, while minimizing the number of drops for the worst case.
Looking at the solution in the Cracking the Coding Interview book I can see the equation for 100 floors as
( X ( X + 1 ) / 2 ) = 100
If we distribute X we are left with a quadratic equation and we can use the quadratic formula to solve for X and use that as our interval. My issue is that I am not sure how that equation is derived in the first place.
The following code solves this problem:
bool drop( int floor ) {
return ( floor >= breakingPoint );
}
int findBreakingPoint( int floors ) {
int interval = 14;
int prevFloor = 0;
int egg1 = interval;
while ( !drop( egg1 ) && egg1 <= floors ) {
interval--;
prevFloor = egg1;
egg1 += interval;
}
int egg2 = prevFloor + 1;
while ( ( egg2 < egg1 ) && ( egg2 <= floors ) && ( !drop( egg2 ) ) ) {
egg2++;
}
return egg2 > floors ? -1 : egg2;
}
Can anyone help me out on this?