Egg Dropping Puzzle

Question:

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 worst case.

 

Solution:

use formula x*(x+1)/2=N.

the answer is 14.
14*15/2=105
therefore 14


Explanation:
1st attempt 14th floor
then 27th floor (14 + 13)
then 39th (14 + 13 + 12)
...
in the worst case you will have to make at max 14 drops!!

 

Follow up:

There is a building of F 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 E eggs. Find the minimum the number of drops required to find the floor from which the egg starts breaking.

 

From Wiki Egg Dropping puzzle we know that the the state transfer equation is:

W(n,k) = 1 + min{ max(W(n − 1, x − 1), W(n,k − x)) } , x = 1, 2, ..., k

W(n,1)=1, W(1,k)=k

n = number of test eggs available

k = number of (consecutive) floors yet to be tested

Below is my understanding.

We have k floors, n eggs, assume we use an egg to test in x floor. there are only two possible results:

  1. it breaks, so the problem recursively come to: x-1 floors, n-1 eggs, which reflects to W(n-1,x-1)
  2. it doesn't break, so the problem recursively come to: k-x floors, n eggs, which reflects to W(n,k-x)

Since the problem requires the worst case, we have to choose the bigger one to ensure the worst case works, that's why we add an max between W(n-1,x-1) and W(n,k-x).

Besides, as we just assumed testing in x floor, x can be from 1 to k, in this situation, we definitely need to choose the minimum to ensure the min experimental drops to find out N, that's why we add an min between {max(W(n − 1, x − 1), W(n,k − x)): x = 1, 2, ..., k}

Finally, as we have used 1 drop in x floor, so the equation must add 1, which reflects to the first part of the equation.

 

Dictionary<Tuple<int,int>,int> lookup = new Dictionary<Tuple<int,int>,int>();

int eggdrop(int floors, int eggs) {
 if(floors==0||floors==1||eggs==1)
  return floors;

 Tuple<int,int> key = new Tuple<int,int>(floors, eggs);
 
 if(lookup.haskey(key))
  return lookup[key];

 int result = Int32.PositiveInfinity;
 for(int i=1;i<=floors;i++) {
  int min_from_this_floor = 
   1 + max( eggdrop(i-1, eggs-1)  //egg breaks from current floor; check all lower floors
   ,  eggdrop(floors-i, eggs) ); //egg doesn't breaks from current floor; check all higher floors

  if(min_from_this_floor < result)
   result = min_from_this_floor;
 }

 lookup[key] = result;
 return result;
}

 

References:

http://puzzlersworld.com/interview-puzzles/100-floors-2-eggs-puzzle/

http://algohub.blogspot.in/2014/05/egg-drop-puzzle.html

http://stackoverflow.com/questions/10177389/generalised-two-egg-puzzle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值