Coursera Algorithms week1 练习测验:Egg drop 扔鸡蛋问题

转自 https://www.cnblogs.com/evasean/p/7208986.html题目原文:Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher an...
摘要由CSDN通过智能技术生成

转自 https://www.cnblogs.com/evasean/p/7208986.html

题目原文:

Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of T given the following limitations on the number of eggs and tosses:

Version 0: 1 egg, ≤T tosses.
Version 1: ∼1lgn eggs and ∼1lgn tosses.
Version 2: ∼lgT eggs and ∼2lgT tosses.
Version 3: 2 eggs and ∼2 n \sqrt{n} n tosses.
Version 4: 2 eggs and ≤c T \sqrt{T} T tosses for some fixed constant c
分析:

version0 : 拿着一个鸡蛋从1~n依次扔就可以,到floor T会碎,故复杂度为≤T

version 1: 采用二分查找,首先从n/2层开始扔:

if(鸡蛋碎) 从(n/2)/2层开始扔;

else 从n/2+(n/2)/2层开始扔

二分方法需要lgn个鸡蛋尝试lgn次

version 2: 依次从1, 2, 4, 8, 16, 32,…2k开始扔,如果鸡蛋在2k碎了,那么2k-1≤T≤2k,这时已经使用了 lgT 次步,接下来在[2k-1+1,2k)区间进行version1的二分查找方法,需要花费lgT步。这两种操作加起来总共花费2lgT步

version 3: 将0~n层楼分成[1, n \sqrt{n} n -1], [ n \sqrt{n} n , 2 n \sqrt{n} n -1], [2 n \sqrt{n} n ,3 n \sqrt{n} n

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值