Problem Statement:
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 2 eggs. Find N, while minimizing the number of drops for the
worse case.
Solution:
Observation: Regardless of how we drop Egg1, Egg2 must do a linear search. Eg, if the Egg1
breaks between floor 10 and 15, we have
to check every floor in between with the Egg2
The Approach:
A First Try: Suppose we drop an egg
from the 10th floor, then the 20th, …
»» In the first egg breaks on the first
drop (Floor 10), then we have at most 10 drops total.
»» If the first egg breaks on the last
drop (Floor 100), then we have at most 19 drops total
(floors 1 through 100, then 91 through
99).
»» That’s pretty good, but all we’re
considered about is the absolute worst case. We should do some “load balancing”
to make those two cases more even.
Goal: Create a system for dropping Egg1
so that the most drops required is consistent,
whether Egg1 breaks on the first drop
or the last drop.
1. A perfectly load balanced system
would be one in which Drops of Egg1 + Drops of
Egg2 is always the same, regardless of
where Egg1 broke.
2. For that to be the case, since each
drop of Egg1 takes one more step, Egg2 is allowed
one fewer step.
3. We must, therefore, reduce the
number of steps potentially required by Egg2 by one drop each time. For
example, if Egg1 is dropped on Floor 20 and then Floor 30, Egg2 is potentially
required to take 9 steps. When we drop Egg1 again, we must reduce potential
Egg2 steps to only 8. eg, we must drop
Egg1 at floor 39.
4. We know, therefore, Egg1 must start
at Floor X, then go up by X-1 floors, then
X-2, …, until it gets to 100.
5. Solve for X+(X-1)+(X-2)+…+1 = 100.
X(X+1)/2 = 100 -> X = 14
We go to Floor 14, then 27, then 39, … This takes 14 steps
maximum.
No comments:
Post a Comment
Your feedback is always appreciated.
I will try to reply to your queries as soon as time allows.
Please do not spam Spam comments will be deleted immediately upon our review.