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'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.

 

次数  阶数  如果破了最坏情况

1      x      x-1

2      y      y-x

3      z      z-y+1

.       .      .

.       .      .

 

x-1 = y-x = z-y+1 ,每次的最坏情况应该是一样的。

次数  阶数  如果破了最坏情况

1      x        x

2      2x-1   x

3      3x-3   x

.       .         .

.       .         .

n      nx-n(n-1)/2   x

令 n = x,则此时nx-n(n-1)/2>=100,因为如果小于100,那么第n次没有破的情况下,已经丢了n次,还得从nx-n(n-1)/2+1次开始丢到100,看是哪一次破的,此时已经丢了n次,即x次了,那么最终的结果将大于x次

因此得到不等式 x*x-x(x-1)/2>=100 得到 x>=13.5

又因为x要尽量小,因此x = 14

转载于:https://www.cnblogs.com/johnsblog/p/3924069.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值