Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing eggs from a building

This is exercise problem 1.4.24 from Robert Sedgewick's Algorithms 4th Edition.

Suppose that you have an N-story building and plenty of eggs. Suppose also that
an egg is broken if it is thrown off floor F or higher, and unhurt otherwise.
First, devise a strategy to determine the value of F such that the number of
broken eggs is ~lgN when using ~lgN throws, then find a way to reduce the cost to
~2lgF.

While the lgN solution is easy to think up, I totally have no idea about the 2lgF solution. Anyway, we are not given the value of F, so where the ground of 2lgF solution is?

Can anyone give some light on this question? Thanks.

like image 295
Guibao Wang Avatar asked Jul 01 '13 12:07

Guibao Wang


People also ask

How do you throw an egg from a building without breaking it?

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.

What does it mean when you drop an egg on the floor?

Dropping and breaking an egg is an announcement of good news. But when the egg is not damaged, or only crack it is considered a bad luck. If you drop the spoon - expect the guest. If you drop the fork - a guest will be a woman.

What is the solution to the problem with 100 storeys and 2 eggs?

Drop the egg from the first-floor window; if it survives, drop it from the second-floor window. Continue upward until it breaks. In the worst case, this method may require 100 droppings.

What is the highest floor you can drop an egg from without it cracking?

A building has 100 floors. One of the floors is the highest floor an egg can be dropped from without breaking. If an egg is dropped from above that floor, it will break. If it is dropped from that floor or below, it will be completely undamaged and you can drop the egg again.


2 Answers

logN: start at the top, always cut search space in half -> binary search

2 * logF start at 1, next 2, 4, 8 (i.e., 2^i), once the egg breaks (after log F steps) do binary search in the smaller search space (range < F and hence number of searches < log F) --> exponential search

like image 182
b.buchhold Avatar answered Oct 21 '22 07:10

b.buchhold


The lg(F) solution is to do 1, 2, 4, 8, ... until the first egg breaks at 2^(k+1), then do a binary search in the range 2^K to 2^(k+1).

Another alternative is to carry out the same process until the first egg breaks at 2^(k+1) then start over except adding 2^k to each height: 2^k + 1, 2^k + 2, 2^k + 4, 2^k + 8, .... You can keep repeating this process to keep reducing the size of the range you do exponential search in.

like image 5
Timothy Shields Avatar answered Oct 21 '22 07:10

Timothy Shields