`

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

分享到:
评论

相关推荐

    C#,动态规划(DP)丢鸡蛋问题(Egg Dropping Puzzle)的三种算法与源代码

    C#,动态规划(DP)丢鸡蛋问题(Egg Dropping Puzzle)的三种算法与源代码 动态规划(Dynamic Programming,DP)是运筹学的一个分支,是求解决策过程最优化的过程。20世纪50年代初,美国数学家贝尔曼(R.Bellman)等...

    Dropping Ball using javascript.zip

    "Dropping Ball using javascript.zip" 提供了一个简单的JavaScript游戏示例,名为“Balldrop”,它利用了JavaScript的核心功能来实现一个动态的球下落效果。这个项目非常适合初学者学习,因为它涉及到基础的HTML、...

    Dropping-Thunder:Dropping Thunders TD模拟器

    【Dropping-Thunder:Dropping Thunders TD模拟器】 Dropping-Thunder是一个基于Java开发的塔防(TD)游戏模拟器,它允许用户创建、编辑并玩转自定义的塔防地图。塔防游戏,全称Tower Defense,是策略游戏的一种,...

    Condorcet with Dual Dropping-开源

    《Condorcet with Dual Dropping:开源选举决策机制解析》 在当代社会,选举和投票是决策过程中的重要环节,特别是在组织内部、社区选举以及更广泛的公共事务中。 Condorcet with Dual Dropping 是一种先进的选举...

    textlint-rule-no-dropping-i:Textlint规则来检查马虎字

    textlint-rule-no-dropping-i这是一个检测丢失单词的规则。 are我们正在发展。 developing我在发展。安装npm install @textlint-ja/textlint-rule-no-dropping-i用法将@textlint-ja/textlint-rule-no-dropping-i ....

    Dropping Apple by airscape-crx插件

    语言:English通过空中景观放下苹果,绝对不会错过重要的掉苹果活动,例如小睡放苹果可以帮助您与商店的预购开店保持最新并保持个人时间表...使用Airscape的Dropping Apple绝不会错过重要的Apple Droping事件,例如小睡

    textlint-rule-no-dropping-the-ra:Textlint规则以检出单词

    人が来れないんです安装npm install textlint-rule-no-dropping-the-ra用法将“ no-drop-the-ra” .textlintrc { " rules " : { " no-dropping-the-ra " : true }}贡献叉吧! 创建功能分支: git checkout -b my-new...

    BallDropping

    BallDropping是一个会让人上瘾的噪音制造器,如果平时无聊的时候也可以当作小游戏来玩。很多小球从天而降,根据碰击角度、速度的不同,掉在鼠标划的线上会发出大珠小珠落玉盘的声音。 &lt;br&gt;你可不要小看了...

    GPS测试工具

    标题中的“GPS测试工具”指的是一个专门用于检测和分析全球定位系统(GPS)性能的应用程序。这类工具通常用于检查GPS接收机的信号质量、定位精度、速度计算以及时间同步等功能。在IT领域,GPS测试工具对于开发、调试...

    Ball-Dropping-Game:827游戏的第一款游戏

    《Ball-Dropping-Game:827游戏的第一款游戏》是一款基于JavaScript开发的趣味小游戏,其核心玩法是操控角色抛掷小球。作为827游戏工作室的首款作品,这款游戏展示了JavaScript在创建互动娱乐体验方面的潜力。下面...

    解决Qt Serialbus 报错3.5char问题源码

    在使用Qt进行串行通信开发时,可能会遇到一个与Qt Serialbus模块相关的错误,提示“dropping older ADU fragments due to larger than 3.5 char”。这个错误通常出现在处理CAN(Controller Area Network)协议的数据...

    dropping-probability.rar_网格计算_Visual_C++_

    标题中的“dropping-probability.rar”暗示了这个项目可能涉及到网络通信中数据包的丢弃概率问题,这通常与网络拥塞控制、流量管理或可靠传输相关。在这个上下文中,“网格计算”是指利用分布式计算资源,如多台...

    Distributed Weighted Fusion Estimators with Random Delays and Packet Dropping

    This paper is concerned with the distributed fusion estimation in sensor networks where local estimates are sent to a fusion centre for fusion estimation, with random delays and packet dropouts....

    TCL-script-to-find-wireless-packet-dropping-nodes_Windows编程_tcl/tk_

    TCL script to find wireless packet dropping nodes2609002STC12C5A60S2+2.4TFT+DS1302+DS18B20+GPS+FM 自动校时收音机电子钟 STC12C5A60S2+2.4TFT+DS1302+DS18B20+GPS+FM 自动校时收音机电子钟 捣鼓了许久

    小学英语绕口令及名言.doc

    "A big egg. Falling down into a bed. Rolling under Tom’s leg. Making him lose his head." 这段绕口令锻炼了孩子们对于元音和辅音的发音,如"big", "egg", "rolling" 和 "leg",同时故事性的情境也增加了趣味性...

    Android代码-仿微信掉落表情包效果

    中文版文档 Emoji Rain Hey, it's raining emoji! This is a really simple and funny animation for Android. You could find similar ...How many emojis will dropping in each flow, default 6 duration

    Energy Harvesting Wireless Sensor Node With Temporal Death

    dropping probabilities, i.e., the packet dropping probability due to energy depletion and that due to channel error. Numerical examples are provided to illustrate the theoretical findings, and new ...

    Dropping-softbody-in-2D:自学项目

    2D滴入式软体 自学项目我一直对计算机模拟物理世界充满热情。 最近,我试图在二维中模拟与刚体碰撞时的软体变形。 该程序仍未完成,但是现在我将其留在这里,过一会儿我将返回它。

    VirtualBox 2.2.0使用主机网络上网配置教程

    当VirtualBox 2.1.4升级到2.2.0以后,突然发现虚拟的系统无法使用主机的网络上网了,google了一下,发现很多人碰到这个问题,但没有解决办法,甚至有人认为是VirtualBox的Bug,其实不然。 经过研究发现,2.2.0缺省...

    42_ZYNQ7020开发板Vivado配置RAM并用Vivao自带逻辑分析仪分析

    RAM是FPGA中常用的基础模块,可广泛用于存储数据,同样它也是ROM,FIFO的基础。 xilinx在Vivao里为我们已经提供了RAM的IP核,我们只需要通过IP核例化一个RAM,根据RAM的读写时序写入和读取RAM中存储的数据。...

Global site tag (gtag.js) - Google Analytics