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 ...
随机推荐
- ADF学习实用网站
ADF中所有组件工功能例子 http://jdevadf.oracle.com/adf-richclient-demo/faces/components/dialog.jspx;jsessionid= ...
- Nmap参数说明
Nmap(Network Mapper)是一款开放源代码的网络探测和安全审核工具.它的设计目标是快速地扫描大型网络,不适应于单一主机.Nmap使用检测IP数据包来确定访问的主机.提供的服务.数据包的类 ...
- 利用ks构建ISO中的一些坑
构建ISO的基本流程 1.获取rpm包源码 2.将源码增量编译成二进制包 3.编写ks的包列表决定ISO制作时需要从什么地方(二进制仓库repo)取哪些二进制包 4.通过createiso命令并指定k ...
- [Groovy] 学习Groovy的好网站(内容全面)
https://www.tutorialspoint.com/groovy/index.htm
- memcached centos启动笔记
root情况下命令行输入 apt-get install memcached 自动安装 不熟悉的情况下 可能找不到改程序所在目录 使用 find / -name memcached 从根目录开始寻找 ...
- maven web 项目 打入 jar 包 , 和编译入 java 文件到 web-inf 下
<outputDirectory>src\main\webapp\WEB-INF\classes</outputDirectory> 可以把 类文件编译到 web-inf 下 ...
- jquery 元素筛选 13.6.20
<ul> <li>list item 1</li> <li>list item 2</li> <li class="thir ...
- 在mac console下 执行c++文件
1 $ g++ -o NewFileName OldFileName.cpp -o is the letter O not zero NewFileName will be your executab ...
- this.closest()在IE中报错的原因及解决办法
closest()定义在jquery中,不能在原生的js中使用 解决方法:将this.closest()换成$(this).closest()即可
- tomcat中如何配置虚拟路径
第一步:打开server.xml配置文件.在Host节点里写上该行代码: <Context path="/upload" docBase="E:\upload&qu ...