Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文:
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$ \sqrt{n} $ tosses.
- Version 4: 2 eggs and ≤c$ \sqrt{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, $ \sqrt{n} $-1], [$ \sqrt{n} $, 2 $ \sqrt{n} $-1], [2$ \sqrt{n} $,3 $ \sqrt{n} $-1]...[k$ \sqrt{n} $, (k+1)$ \sqrt{n} $-1]..个区间,用一个鸡蛋分布从1开始在各个区间的起始楼层扔,如果在k$ \sqrt{n} $层碎了,那就从(k-1)$ \sqrt{n} $+1开始逐层扔。第一步区间选择用了 $ \sqrt{n} $的复杂度,第二步区间内部扔鸡蛋用了 $ \sqrt{n} $的复杂度,总共用了 2$ \sqrt{n} $
version 4: 尝试从1, 4, 9, 16, 25,...(k-1)2, k2....楼层扔鸡蛋,加入鸡蛋在楼层k2碎了,意味着(k-1)2≤T≤k2,这一步尝试了$ \sqrt{T} $次(k=$ \sqrt{T} $)。接着从楼层(k-1)2+1开始逐层扔,最多尝试至k2-1结束,这一步需要尝试k2-1-(k-1)2-1=2$ \sqrt{T} $-1=2$ \sqrt{T} $-2次。总共用了3$ \sqrt{T} $-2次
Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题的更多相关文章
- Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time
题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...
- Leetcode 887 Super Egg Drop(扔鸡蛋) DP
这是经典的扔鸡蛋的题目. 同事说以前在uva上见过,不过是扔气球.题意如下: 题意: 你有K个鸡蛋,在一栋N层高的建筑上,被要求测试鸡蛋最少在哪一层正好被摔坏. 你只能用没摔坏的鸡蛋测试.如果一个鸡蛋 ...
- [LeetCode] 887. Super Egg Drop 超级鸡蛋掉落
You are given K eggs, and you have access to a building with N floors from 1 to N. Each egg is iden ...
- Coursera Algorithms week1 查并集 练习测验:3 Successor with delete
题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...
- Coursera Algorithms week1 查并集 练习测验:2 Union-find with specific canonical element
题目原文: Add a method find() to the union-find data type so that find(i) returns the largest element in ...
- Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...
- Coursera Algorithms week3 快速排序 练习测验: Decimal dominants(寻找出现次数大于n/10的元素)
题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occ ...
- Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)
题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respec ...
- Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts
题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is t ...
随机推荐
- 学习笔记-java IO流总结 转载
1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...
- POJ 3070 - 快速矩阵幂求斐波纳契数列
这题并不复杂. 设$A=\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}$ 由题中公式: $\begin{pmatrix}f(n+1) & ...
- codeforces_738D
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- accmcolor
accmcolor c; c.setcolorindex(1); playertablerecord.setcolor(c);
- CAD类型转换
AcDbEntity *pEnt; AcDbCircle *pcir = AcDbCircle::cast(pEnt); static_cast<AcDbCircle*>(pEnt); p ...
- Learning opencv续不足(七)线图像的设计D
因为线图像startline有了起点和终点,我们就可以用DDA法求出线上所有点,任意斜率直线通过四象限八区域查表法界定.我们只示范一个区域:函数为: public PointF DdaFindPtIm ...
- JAVA经典题--计算一个字符串中每个字符出现的次数
需求: 计算一个字符串中每个字符出现的次数 思路: 通过toCharArray()拿到一个字符数组--> 遍历数组,将数组元素作为key,数值1作为value存入map容器--> 如果k ...
- zabbix4.0搭建(基于CentOS6.8)
环境 服务端:188.188.3.241,系统:centos6.8,mysql:5.7.3,php:5.4.9,nginx:1.12.0 一.nginx编译安装 NGINX_VERSION=1.1 ...
- gitblit 搭建本地git服务器
本文主要描述gitblit搭建本地服务器
- python基础综合题----选自python二级考试
<笑傲江湖>是金庸的重要武侠作品之一.这里给出一个<笑傲江湖>的网络版本, 文件名为“笑傲江湖-网络版.txt”. ...