100 floors 2 eggs
Phone Screen - SOLUTION
Problem
A tower has 100 floors. You've been given two eggs. The eggs are strong enough that they can be dropped from a particular floor in the tower without breaking. You've been tasked to find the highest floor an egg can be dropped without breaking, in as few drops as possible. If an egg is dropped from above its target floor it will break. If it is dropped from that floor or below, it will be intact and you can test drop the egg again on another floor.
Show algorithmically how you would go about doing this in as few drops as possible
Requirements
Use paper/pencil or a whiteboard for this problem
Solution
We've already seen this problem in the Riddles section, here is the answer from that section.(Alternatively just google "2 eggs 100 floors" for a plethora of explanations regarding this same solution
Start from the 10th floor and go up to floors in multiples of 10.
If first egg breaks, say at 20th floor then you can check all the floors between 11th and 19th with the second egg to see which floor it will not break.
In this case, the worst-case number of drops is 19. If the threshold was 99th floor, then you would have to drop the first egg 10 times and the second egg 9 times in linear fashion.
Best solution: We need to minimize this worst-case number of drops. For that, we need to generalize the problem to have n floors. What would be the step value, for the first egg? Would it still be 10? Suppose we have 200 floors. Would the step value be still 10?
The point to note here is that we are trying to minimize the worst-case number of drops which happens if the threshold is at the highest floors. So, our steps should be of some value which reduces the number of drops of the first egg.
Let's assume we take some step value m initially. If every subsequent step is m-1, then,
This is
If n =100, then m would be 13.65 which since we can't drop from a decimal of a floor, we actually use 14.
So, the worst case scenario is now when the threshold is in the first 14 floors with number of drops being 14.
Note that this is simply a binary search!
Good Job!
100 floors 2 eggs的更多相关文章
- [CareerCup] 6.5 Drop Eggs 扔鸡蛋问题
6.5 There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. I ...
- 《Cracking the Coding Interview》——第6章:智力题——题目5
2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...
- Cracking the Coding Interview 6.5
There is a building of 100 floors. If an egg drops from the Nth floor or above, it will break. If it ...
- HDU 3016 线段树区间更新+spfa
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 495A - Digital Counter
A. Digital Counter time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Javascript Notes
Get started with Programming Javascript中的保留字,但是大部分却没在这个语言中使用.undefined,NaN,Infinity也应该算保留字. abstract ...
- Codeforces Round #282 (Div. 2) A
解题思路:用数组将每一个显示数字可能表示的数字种数存储起来即可 反思:第一次做的时候没有做出来是因为题意理解错误,第二次WA是因为情况没有考虑完全,1对应有7个数字,5对应有4个数字 A. ...
随机推荐
- icil 参考docker
@All 有不知道怎么用docker发布项目的,请参考 http://192.168.18.224:8888/svn/Enterprise/site/docker/overview of docker ...
- eval是只读数据,bind是可更新的.
1.Eval和Bind的区别 绑定表达式 <%# Eval("字段名") %> <%# Bind("字段名") %> Eval(& ...
- MS SQL 2005 无法建立用户实例
SC.EXE stop "MSSQL$SQLEXPRESS"RD /S /Q "%USERPROFILE%\Local Settings\Application Data ...
- 八月(The Summer is Gone)观后感
第一次看到这部电影时觉得很亲近,黑白画面,国企改革的背景,浓浓的儿时画面感,原谅我只是一个三十不到的人,可能我比较早熟,对八九十年代还有些记忆,更早以前也通过电视.音乐.书籍等了解过一些,而那些听过又 ...
- 提取linux中eth0的IP地址
法1:cut [root@oldboy oldboy]# ifconfig eth0|grep 'inet addr'|cut -d ":" -f2|cut -d " & ...
- 复制CentOS虚拟机网络配置
复制出来的CentOS虚拟机,网络需要重新配置. 卸载原来的VMware网卡,重新启用一块新的网卡,网卡网段要匹配. ifconfig -a 查看当前启用网卡的mac地址 编辑/etc/ude ...
- session会话管理,与过滤器使用,访问控制
1 用户登录,是否注册用户,在登录处理页面进行用户验证,创建session保存用户名和密码 2否,进入用户注册页面 3是,系统保存该用户的登录信息 4进入要访问的页面 5用户直接访问某个页面, 6系统 ...
- 在命令提示符下启动并使用JVM时,简单设置堆
公司电脑内存太小了,只有8G:或者说JVM默认启动占用内存太大了,同时启动多个服务内存就炸了. 比如: java -Xmx128m -Xms64m -Xmn32m -Xss16m -jar eurek ...
- docker registry2
https://blog.csdn.net/mideagroup/article/details/52052618
- MySql初步II
[MySql初步II] 1.Order By 你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列. 默认情况下,它是按升排列. 实例: 2.Join语法 Join不是一个关键字 ...