887. Super Egg Drop
You are given
Keggs, and you have access to a building withNfloors from1toN.Each egg is identical in function, and if an egg breaks, you cannot drop it again.
You know that there exists a floor
Fwith0 <= F <= Nsuch that any egg dropped at a floor higher thanFwill break, and any egg dropped at or below floorFwill not break.Each move, you may take an egg (if you have an unbroken one) and drop it from any floor
X(with1 <= X <= N).Your goal is to know with certainty what the value of
Fis.What is the minimum number of moves that you need to know with certainty what
Fis, regardless of the initial value ofF?
Example 1:
Input: K = 1, N = 2
Output: 2
Explanation:
Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Hence, we needed 2 moves in the worst case to know what F is with certainty.Example 2:
Input: K = 2, N = 6
Output: 3Example 3:
Input: K = 3, N = 14
Output: 4
Note:
1 <= K <= 1001 <= N <= 10000
Approach #1: DP. [C++][TLE][O(K*N^2)]
class Solution {
public:
int superEggDrop(int K, int N) {
int c = 0;
vector<vector<int>> dp(K+1, vector<int>(N+1, 0));
for (int i = 1; i <= N; ++i) dp[1][i] = i;
for (int i = 2; i <= K; ++i) {
for (int j = 1; j <= N; ++j) {
dp[i][j] = INT_MAX;
for (int k = 1; k <= j; ++k) {
c = 1 + max(dp[i-1][k-1], dp[i][j-k]);
if (c < dp[i][j])
dp[i][j] = c;
}
}
}
return dp[K][N];
}
};
Approach #2: DP. [Java]
class Solution {
public int superEggDrop(int K, int N) {
int[][] dp = new int[N+1][K+1];
int m = 0;
while (dp[m][K] < N) {
++m;
for (int k = 1; k <= K; ++k)
dp[m][k] = dp[m-1][k-1] + dp[m-1][k] + 1;
}
return m;
}
}
Analysis:
Firstly, if we have K eggs and s steps to detect a buliding with Q(k, s) floors.
Secondly, we use 1 egg and 1 step to detect one floor,
if egg break, we can use (k-1) eggs and (s-1) to detect with Q(k-1, s-1),
if egg isn't broken, we can use k eggs and (s-1) step to detech with Q(k, s-1),
So, Q(k,s) = 1 + Q(k, s-1) + Q(k-1, s-1);
dp[i] is max floors we can use i eggs and s step to detect.
Reference:
https://leetcode.com/problems/super-egg-drop/discuss/159508/easy-to-understand
887. Super Egg Drop的更多相关文章
- [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 ...
- 【LeetCode】887. Super Egg Drop 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...
- Leetcode 887 Super Egg Drop(扔鸡蛋) DP
这是经典的扔鸡蛋的题目. 同事说以前在uva上见过,不过是扔气球.题意如下: 题意: 你有K个鸡蛋,在一栋N层高的建筑上,被要求测试鸡蛋最少在哪一层正好被摔坏. 你只能用没摔坏的鸡蛋测试.如果一个鸡蛋 ...
- LeetCode 887. Super Egg Drop
题目链接:https://leetcode.com/problems/super-egg-drop/ 题意:给你K个鸡蛋以及一栋N层楼的建筑,已知存在某一个楼层F(0<=F<=N),在不高 ...
- [Swift]LeetCode887. 鸡蛋掉落 | 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 ident ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- 膜 社论(egg drop)
题面 \(n\) 楼 \(m\) 个鸡蛋,从 \(k\) 楼及以上扔下去会碎,不能再测试 . 问至少需要扔几次确定 \(k\) . \(n\le 10^{18}\),\(m\le 64\) . 题解 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- VMware安装centos虚拟机 通过NAT与主机互通并能上网
1.关于centos虚拟机的安装,我这里就不详细说明了,网上有很多教程,默认你们已经安装好. (我的环境是centos6.6 x86 最小安装版) 2.右键虚拟主机,选择设置选项. 3.在 ...
- 在Struts2框架中使用OGNL表达式(在jsp页面中使用OGNL表达式)
1. Struts2引入了OGNL表达式,主要是在JSP页面中获取值栈中的值 2. 具体在Struts2中怎么使用呢?如下步骤 * 需要先引入Struts2的标签库(在JSP页面的最上面位置) > ...
- asp.net core web 本地iis开发
发布后打开没有问题,直接配置到本地iis时,会提示如下错误 HTTP Error 502.5 - Process Failure
- 求N的阶乘N!中末尾0的个数
求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...
- UI设计初学者如何避免走弯路?
对于初学UI设计的人而言,可能对UI具体是做什么,或者自己是否能顺利转行胜任这样的岗位存在一定的顾虑,今天我们就来重点说说UI是做什么的,以及想学UI到底要如何避免走弯路,快速的学成. 问题一:UI设 ...
- Fully Update And Upgrade Offline Debian-based Systems
Let us say, you have a system (Windows or Linux) with high-speed Internet connection at work and a D ...
- scrapy官方安装方法
安装依赖 sudo apt-get install python-dev python-pip libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libss ...
- C和C++之间库的互相调用
http://www.cppblog.com/wolf/articles/77828.html 昨晚有个朋友问我关于在C中调用C++库的问题,今天午饭后,由于脖子痛的厉害而没有加入到我们组的“每天一战 ...
- SQLALchemy--ORM框架
SQLAlchemy 1.介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用 ...
- KBMMW 4.6 正式版发布
喜大普奔迎新年! Merry Christmas! We are happy to announce the release of kbmMW v. 4.60.00 Professional and ...