I am reading Robert Sedgewick's algorithms 4th edition book, and he has the following task:
Suppose that you have an N-story building and 2 eggs. Suppose also that an egg is broken if it is thrown off floor F or higher, and unbroken otherwise. Your cost model is the number of throws. Devise a strategy to determine F such that the number of throws ~c √ F for some constant c.
The first part of the task is to find F in 2 √ N steps, and here's a solution for that:
Solution to Part 1:
He also provides a hint for the ~c √ F part (part 2):
Hint for Part 2: 1 + 2 + 3 + ... k ~ 1/2 k^2.
So what is the algorithm to do it in ~c √ F steps?
We see then that the first egg should be thrown finally from the 99th floor, previously from 99–2=97, previously from 97–3=94, 90, 85, 79, 72, 64, 55, 45, 34, 22 and the 9th floor. This is an optimal solution!
First solution is linear search strategy which is very straightforward if we don't care about number of drops. We can start dropping egg from floor 1, floor 2, floor 3 and continue till floor 100 until we find the floor N. In worst case it will take 100 drops if egg breaks only at the last (100th) floor.
Answer: The easiest way to do this would be to start from the first floor and drop the egg. If it doesn't break, move on to the next floor. If it does break, then we know the maximum floor the egg will survive is 0.
To drop an egg without breaking it, wrap the egg in wet paper towels and place it in a plastic bag of puff rice cereal. Fill 4 small bags with puffed cereal too, then put all the bags into 1 large container. You can also wrap the egg in packing material, like bubble wrap, packing peanuts, or inflated plastic packets.
Drop first egg from floors 1, 3, 6, ... (the partials sums of 1, 2, 3, ...). Then do linear search between last two floors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With