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 floorsif(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

Egg Dropping Puzzle相关推荐

  1. c++扔鸡蛋问题egg dropping puzzle(附完整源码)

    C++扔鸡蛋问题egg dropping puzzle 扔鸡蛋问题egg dropping puzzle算法的完整源码(定义,实现,main函数测试) 扔鸡蛋问题egg dropping puzzle ...

  2. Egg Dropping Puzzle问题的分析

    首先,基本问题是这样:You are given two eggs, and access to a 100-storey building. The aim is to find out the h ...

  3. 扔鸡蛋问题具体解释(Egg Dropping Puzzle)

    经典的动态规划问题,题设是这种: 假设你有2颗鸡蛋,和一栋36层高的楼,如今你想知道在哪一层楼之下,鸡蛋不会被摔碎,应该怎样用最少的測试次数对于不论什么答案楼层都可以使问题得到解决. 假设你从某一层楼 ...

  4. Eggs Dropping puzzle(2 eggs, 100 floors)

    题目如下: You are given two eggs, and access to a 100-storey building. Both eggs are identical. The aim ...

  5. 计算机科学精彩帖子收集

    linux源码 LXR 源自"the Linux Cross Referencer",中间的"X"形象地代表了"Cross".与 Sourc ...

  6. 动态规划编程面试_面试的前25大动态编程问题

    动态规划编程面试 Dynamic programming is one of the most asked paradigms in any product-based company intervi ...

  7. 婚礼请帖_第一次在线婚礼发生在1876年

    婚礼请帖 重点(Top highlight) As the Covid-19 pandemic wears on, more and more of life has moved online - s ...

  8. C和C++算法完整教程专栏完整目录

    C和C++算法完整教程专栏完整目录 专栏说明如下 完整专栏目录如下 专栏说明如下 内容:C和C++算法完整教程 数量:680篇博文(2023年2月15日截止) 更新时间至:2023年2月15日(后续加 ...

  9. dp 扔鸡蛋_使用动态编程(DP)的鸡蛋掉落问题

    dp 扔鸡蛋 Problem statement: You are given N floor and K eggs. You have to minimize the number of times ...

最新文章

  1. c++ mqtt客户端_MQTT安全性设计详解
  2. SAP RETAIL 根据Allocation Rule去创建分配表
  3. Example017简单的下拉框
  4. 高清变脸更快更逼真!比GAN更具潜力的可逆生成模型来了 | OpenAI论文+代码
  5. 结对子作业 四则运算 V2.0
  6. 程序员过关斩将--请不要误会redis 6.0 的多线程
  7. C# 客户端内存优化分析
  8. P2825 [HEOI2016/TJOI2016]游戏
  9. 【提醒】刷脸取件被小学生“破解”!丰巢紧急下线
  10. 自实现进程管理器linux,【Linux工具篇】supervisor进程管理器
  11. word 远程过程调用失败。 (异常来自 HRESULT:0x800706BE) 解决方法
  12. 网络爬虫与HTTP协议
  13. 怎样回答面试题更好?以及注意事项
  14. 一篇文章让你了解互联网公司的职位架构以及职能
  15. Java反射的底层原理,以及Java反射的性能分析及优化
  16. 向上转型 (Upcasting)
  17. [机器学习笔记] 用Python分析 TED演讲数据(更新中)
  18. KMP算法——很详细的讲解
  19. 动态因子模型的理论和应用研究读书笔记(朱满洲)
  20. Linux个性化桌面,颜值即正义,超好用的 Linux 桌面个性化工具推荐

热门文章

  1. Openlayers使用geoserver发布的图层
  2. excel分类汇总的使用
  3. iPhone详尽介绍与评测
  4. Java Web简明教程–开发环境介绍[2]
  5. C/C++网络通讯编程(一)
  6. 用C语言编程画出图形,C语言图形编程(六) -图形程序设计实例:零件图形的绘制...
  7. 只有0.7M?超超超轻量SOTA目标检测模型揭秘!
  8. [置顶] 最近要强迫自己了解的东西
  9. 经典话语-20131116
  10. html命令代码大全,一、HTML各种命令的代码: